[英]Mosquitto java broker is showing error: Exception in thread “main” java.lang.RuntimeException: Can't locate the resource “config/moquette.conf”
I want to experiment 'mqtt' protocol. 我想尝试“ mqtt”协议。 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/ .
为此,我已经下载了此链接中介绍的必要文件http://www.hascode.com/2016/06/playing-around-with-mqtt-and-java-with-moquette-and-eclipse-paho/ 。
Code: 码:
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)
I cannot understand this error message. 我无法理解此错误消息。 I have already installed mosquito in C:\\Program Files (x86)\\mosquitto.
我已经在C:\\ Program Files(x86)\\ mosquitto中安装了蚊子。 I have tested it using Publisher:
我已经使用发布服务器对其进行了测试:
mosquitto_pub -m "message from mosquitto_pub client" -t "test"
and Subscriber: 和订阅者:
mosquitto_sub -t "test".
Please give me advise to solve this problem. 请给我建议解决这个问题。
The error is showing because bin, config and lib folder are not added to the Maven project ( read Ready 'Broker' part). 由于未将bin,config和lib文件夹添加到Maven项目中,因此显示了错误(请参阅“就绪”部分)。
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 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 Client: https://www.eclipse.org/paho/clients/java/ Eclipse Paho Java客户端: 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 下载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
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.
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.