1

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

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

链接到 HiveMQ GitHub:

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

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

在此处输入图像描述

米

在此处输入图像描述

Main.Java 中产生错误的代码

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);
    }


    }

}
4

1 回答 1

1

我的构建路径中没有 HiveMQ 客户端。在出现红色错误的行中,Eclipse 给了我修复项目设置的选项,我单击它,它自动将 HiveMQ 客户端添加到构建路径中。我在下面发布了一个屏幕截图。在此处输入图像描述

于 2019-06-07T17:01:18.967 回答