user1850484
user1850484

Reputation: 388

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. 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/.

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. 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.

Upvotes: 0

Views: 985

Answers (1)

user1850484
user1850484

Reputation: 388

The error is showing because bin, config and lib folder are not added to the Maven project ( read Ready ‘Broker’ part).

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 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

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.

Upvotes: 1

Related Questions