java - 如何使用 Gradle 将 HiveMQ 客户端添加为 HiveMQ 社区版的依赖项?

标签 java eclipse gradle hivemq

我想将 HiveMQ 客户端和 HiveMQ 社区版(代理的实现)合并到一个项目中。我尝试将 HiveMQ 客户端添加为 Hive MQ 社区版(代理)中的 build.gradle 文件的依赖项。它能够成功构建,但我不确定我是否正确地构建了它。当我尝试引用社区版中的客户端类时,它给了我错误。我错过了什么吗?我希望能够将客户端项目放入代理社区版本中,并能够创建客户端并访问 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

enter image description here

M

enter image description here

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 客户端添加到构建路径中。我在下面发布了截图。 enter image description here

关于java - 如何使用 Gradle 将 HiveMQ 客户端添加为 HiveMQ 社区版的依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498031/

相关文章:

android - 任务 ':app:proccessDebugGoogleServices' 执行失败

java - 如何延迟从 gwt 调用数据库?

Java:将 StreamResult 转换为 DOM

c - 如何使用 DDD 查找进程卡住的位置

java - 在Eclipse中创建jar文件时出错

android - Android Studio-Gradle同步失败-groovy-all-2.4.4.jar

android - 将 android gradle 插件更新到 7.0.2 后,Firebase crashlytics 构建失败

java - ListSelectionListener 调用 setSelected 方法时不会触发事件

java - java中是否有相当于C#中DataSet的类型?

java - 使用 @NonNullByDefault 和数组时如何删除 'nullness annotation is redundant' 警告? (Eclipse、空注释)