首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Gradle将HiveMQ客户端作为依赖项添加到HiveMQ社区版?

如何使用Gradle将HiveMQ客户端作为依赖项添加到HiveMQ社区版?
EN

Stack Overflow用户
提问于 2019-06-08 00:19:52
回答 1查看 144关注 0票数 1

我想将HiveMQ Client和HiveMQ社区版(代理的实现)合并到一个项目中。我尝试将HiveMQ客户机作为依赖项添加到Hive MQ Community Edition (代理)中的build.gradle文件中。它能够成功地构建,但我不确定我是否做得正确。当我试图在Community Edition中引用客户机类时,它给了我错误。我是不是遗漏了什么?我希望能够将客户端项目放入代理社区版本中,并能够创建客户端并访问HiveMQ客户端中的所有类。我留下了来自HiveMQ客户端网站、链接以及HiveMQ社区版的build.gradle文件的说明。

我得到的错误:无法解析导入com.hivemq.client (所有引用HiveMQ客户端项目中的任何内容的导入都会发生)

指向HiveMQ GitHubs的链接:

https://github.com/hivemq/hivemq-mqtt-client

https://github.com/hivemq/hivemq-community-edition

Main.Java中产生错误的代码

代码语言:javascript
复制
package com.main;

import java.util.UUID;
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient.Mqtt5Publishes;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import java.util.logging.Logger;
import java.nio.ByteBuffer;
import java.util.Optional;

import java.util.logging.Level;
import java.util.concurrent.TimeUnit;




public class Main {

    private static final Logger LOGGER = Logger.getLogger(Main.class.getName());  // Creates a logger instance 


    public static void main(String[] args) {

                // Creates the client object using Blocking API 

            Mqtt5BlockingClient client1 = Mqtt5Client.builder()
            .identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client. The ID is randomly generated between 
            .serverHost("0.0.0.0")  // the host name or IP address of the MQTT server. Kept it 0.0.0.0 for testing. localhost is default if not specified.
            .serverPort(1883)  // specifies the port of the server
            .buildBlocking();  // creates the client builder

            client1.connect();  // connects the client
            System.out.println("Client1 Connected");


            String testmessage = "How is it going";
            byte[] messagebytesend = testmessage.getBytes();   // stores a message as a byte array to be used in the payload 

    try {

        Mqtt5Publishes publishes = client1.publishes(MqttGlobalPublishFilter.ALL);  // creates a "publishes" instance thats used to queue incoming messages

            client1.subscribeWith()  // creates a subscription 
            .topicFilter("test/topic")
            .send();
            System.out.println("The client has subscribed");

            client1.publishWith()  // publishes the message to the subscribed topic 
            .topic("test/topic")
            .payload(messagebytesend)
            .send();

         Mqtt5Publish receivedMessage = publishes.receive(5,TimeUnit.SECONDS).orElseThrow(() -> new RuntimeException("No message received.")); // receives the message using the "publishes" instance 
         LOGGER.info("Recieved: " + receivedMessage); 


         byte[] getdata = receivedMessage.getPayloadAsBytes();
         System.out.println(getdata.toString());
         System.out.println(receivedMessage);


    }

    catch (Exception e) {    // Catches all exceptions using the "base exception" 
        LOGGER.log(Level.SEVERE, "Something went wrong.", e);
    }


    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-08 01:01:19

我的构建路径中没有HiveMQ客户端。在带有红色错误的行上,Eclipse为我提供了修复项目设置的选项,我单击它,它会自动将HiveMQ客户机添加到构建路径。我在下面发布了一个截图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56498031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档