我想将HiveMQ客户程序和HiveMQ社区版(该代理的实现)结合到一个项目中。我尝试将HiveMQ客户端作为对Hive MQ Community Edition(代理)中build.gradle文件的依赖项添加。它能够成功构建,但是我不确定是否正确完成了构建。当我尝试在Community Edition中引用客户端类时,它给了我错误。我想念什么吗?我希望能够将客户端项目放到Broker社区版本中,并能够创建客户端并访问HiveMQ客户端中所有的类。我离开了HiveMQ客户端网站上的说明,链接以及build.gradle文件的外观,就像HiveMQ社区版一样。

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

链接到HiveMQ GitHub:

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

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

java - 如何使用Gradle将HiveMQ Client添加为HiveMQ Community Edition的依赖项?-LMLPHP

java - 如何使用Gradle将HiveMQ Client添加为HiveMQ Community Edition的依赖项?-LMLPHP

java - 如何使用Gradle将HiveMQ Client添加为HiveMQ Community Edition的依赖项?-LMLPHP

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


    }

}

最佳答案

我的构建路径中没有HiveMQ客户端。在出现红色错误的行中,Eclipse给了我修复项目设置的选项,然后单击它,它自动将HiveMQ客户端添加到构建路径。我在下面发布了屏幕截图。 java - 如何使用Gradle将HiveMQ Client添加为HiveMQ Community Edition的依赖项?-LMLPHP

10-04 13:14