gpt4 book ai didi

java - Mosquitto java 代理显示错误 : Exception in thread "main" java. lang.RuntimeException:无法找到资源 "config/moquette.conf"

转载 作者:行者123 更新时间:2023-12-02 11:18:19 24 4
gpt4 key购买 nike

我想尝试“mqtt”协议(protocol)。为此,我已经下载了此链接http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/中解释的必要文件。 。

代码:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import io.moquette.interception.AbstractInterceptHandler;
import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.InterceptPublishMessage;
import io.moquette.server.Server;
import io.moquette.server.config.ClasspathConfig;
import io.moquette.server.config.IConfig;

public class s1
{

static class PublisherListener extends AbstractInterceptHandler {
@Override
public void onPublish(InterceptPublishMessage message) {
System.out.println("moquette mqtt broker message intercepted, topic: " + message.getTopicName()
+ ", content: " + new String(message.getPayload().array()));
}

public static void main(String args[]) throws IOException
{
// Creating a MQTT Broker using Moquette
final IConfig classPathConfig = new ClasspathConfig();

final Server mqttBroker = new Server();
final List<? extends InterceptHandler> userHandlers = Arrays.asList(new PublisherListener());
mqttBroker.startServer(classPathConfig, userHandlers);

System.out.println("moquette mqtt broker started, press ctrl-c to shutdown..");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("stopping moquette mqtt broker..");
mqttBroker.stopServer();
System.out.println("moquette mqtt broker stopped");
}
});
}
}
}

但是,当我尝试编译时,它显示此错误。

Exception in thread "main" java.lang.RuntimeException: Can't locate the resource "config/moquette.conf"
at io.moquette.server.config.ClasspathConfig.<init>(ClasspathConfig.java:42)
at mytest.s1$PublisherListener.main(s1.java:27)

我无法理解此错误消息。我已经在 C:\Program Files (x86)\mosquitto 中安装了蚊子。我已经使用它进行了测试发布者:

mosquitto_pub -m "message from mosquitto_pub client" -t "test"

和订阅者:

mosquitto_sub -t "test".

请给我建议来解决这个问题。

最佳答案

显示错误是因为 bin、config 和 lib 文件夹未添加到 Maven 项目中(请阅读 Ready ‘Broker’ 部分)。

Java MQTT 轻量级代理:https://github.com/andsel/moquette下载jar文件 https://bintray.com/artifact/download/andsel/generic/distribution-0.10-bundle-tar.tar.gz

Eclipse Paho Java 客户端:https://www.eclipse.org/paho/clients/java/
下载jar文件https://repo.eclipse.org/content/repositories/paho/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.0.2/org.eclipse.paho.client.mqttv3-1.0.2.jar

阅读本教程。 http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/使用此处的示例代码代理和发布者代码。

Ready ‘Broker’ part
1. Create Eclipse maven project.
2. Download distribution-0.10-bundle-tar.tar.gz from https://bintray.com/artifact/download/andsel/generic/distribution-0.10-bundle-tar.tar.gz
It contains bin, config and lib folder.
3. Copy these 3 folders in maven project
4. Compile it.

Ready ‘Publisher’ part
1. Create Eclipse java project.
2. Add org.eclipse.paho.client.mqttv3-1.0.2.jar
3. Compile it.

Ready ‘Subscriber’ part
1. Create Eclipse java project.
2. Add org.eclipse.paho.client.mqttv3-1.0.2.jar
3. Compile it.

关于java - Mosquitto java 代理显示错误 : Exception in thread "main" java. lang.RuntimeException:无法找到资源 "config/moquette.conf",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50111933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com