Mosquitto Java代理显示错误:线程"main"中出现异常. java.lang.RuntimeException:找不到资源"config/moquette.conf". [英] Mosquitto java broker is showing error: Exception in thread "main" java.lang.RuntimeException: Can't locate the resource "config/moquette.conf"
问题描述
我想尝试"mqtt"协议.为此,我下载了此链接中解释的必要文件
I want to experiment ‘mqtt’ protocol. For this I have downloaded necessary file explained in this link 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");
}
});
}
}
}
但是,当我尝试编译时,它显示此错误.
However, when I try to compile, it shows this error.
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中安装了蚊子.我已经使用进行了测试 发布者:
I cannot understand this error message. I have already installed mosquito in C:\Program Files (x86)\mosquitto. I have tested it using Publisher:
mosquitto_pub -m "message from mosquitto_pub client" -t "test"
和订阅者:
mosquitto_sub -t "test".
请给我建议以解决此问题.
Please give me advise to solve this problem.
推荐答案
由于未将bin,config和lib文件夹添加到Maven项目中,所以显示了错误(请参阅代理"部分).
The error is showing because bin, config and lib folder are not added to the Maven project ( read Ready ‘Broker’ part).
Java MQTT轻量级代理: https://github.com/andsel/moquette 下载jar文件 https://bintray.com/artifact/下载/andsel/generic/distribution-0.10-bundle-tar.tar.gz
Java MQTT lightweight broker: https://github.com/andsel/moquette Download jar file 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文件
Eclipse Paho Java Client: https://www.eclipse.org/paho/clients/java/
Download jar file 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/从此处使用示例代码代理和发布者代码.
Read this tutorial. http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ Use example code broker and publisher code from here.
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.
这篇关于Mosquitto Java代理显示错误:线程"main"中出现异常. java.lang.RuntimeException:找不到资源"config/moquette.conf".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!