如何使用 Gradle 添加 HiveMQ Client 作为 HiveMQ Community Edition 的依赖项?
How to add HiveMQ Client as a dependency to HiveMQ Community Edition using Gradle?
我想将 HiveMQ Client 和作为代理实现的 HiveMQ Community Edition 合并到一个项目中。我尝试将 HiveMQ 客户端作为依赖项添加到 Hive MQ 社区版(代理)中的 build.gradle 文件。它能够成功构建,但我不确定我是否正确构建。当我尝试在 Community Edition 中引用客户端 类 时,它给了我错误。我错过了什么吗?我希望能够将客户端项目放入代理社区版本中,并能够创建一个客户端并访问我在 HiveMQ 客户端中可以访问的所有 类。我留下了来自 HiveMQ 客户端网站的说明、链接以及 build.gradle 文件看起来像 HiveMQ 社区版的内容。
我得到的错误:无法解析导入 com.hivemq.client(发生在所有引用 HiveMQ 客户端项目中的任何内容的导入)
Link 到 HiveMQ GitHubs:
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);
}
}
}
我的构建路径中没有 HiveMQ 客户端。在出现红色错误的那一行,Eclipse 给了我修复项目设置的选项,我点击它,它自动将 HiveMQ 客户端添加到构建路径。我在下面贴了一张截图。
我想将 HiveMQ Client 和作为代理实现的 HiveMQ Community Edition 合并到一个项目中。我尝试将 HiveMQ 客户端作为依赖项添加到 Hive MQ 社区版(代理)中的 build.gradle 文件。它能够成功构建,但我不确定我是否正确构建。当我尝试在 Community Edition 中引用客户端 类 时,它给了我错误。我错过了什么吗?我希望能够将客户端项目放入代理社区版本中,并能够创建一个客户端并访问我在 HiveMQ 客户端中可以访问的所有 类。我留下了来自 HiveMQ 客户端网站的说明、链接以及 build.gradle 文件看起来像 HiveMQ 社区版的内容。
我得到的错误:无法解析导入 com.hivemq.client(发生在所有引用 HiveMQ 客户端项目中的任何内容的导入)
Link 到 HiveMQ GitHubs:
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);
}
}
}
我的构建路径中没有 HiveMQ 客户端。在出现红色错误的那一行,Eclipse 给了我修复项目设置的选项,我点击它,它自动将 HiveMQ 客户端添加到构建路径。我在下面贴了一张截图。