-1

Suppose I have 2 different projects. For simplicity each project contains one java class. How do i get them publish and subscribe using MqttClient in java. Here is my code and the message is publishing but not received by the other class. Fist I created a TopicPublisher class which connects to a Mqtt Broker and sends a message. Then I created a TopicSubscriber class which connects to the same broker and subscribe to the topic. But even if the Topic publisher is publishing the message but the other class is not giving any output.NOTE: the two class is in completely different folder in different project.What am I doing wrong here? Here is my code:

public class TopicPublisher{
    public static void main(String[] args) {

    String topic        = "MQTT/Examples";
    String content      = "Message from MqttPublishSample";
    int qos             = 2;
    String broker       = "tcp://iot.eclipse.org:1883";
    String clientId     = "JavaSample";
    MemoryPersistence persistence = new MemoryPersistence();

    try {
        MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        System.out.println("Connecting to broker: "+broker);
        sampleClient.connect(connOpts);
        System.out.println("Connected");
        System.out.println("Publishing message: "+content);
        MqttMessage message = new MqttMessage(content.getBytes());
        message.setQos(qos);
        sampleClient.publish(topic, message);
        System.out.println("Message published");
    } catch(MqttException me) {
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        me.printStackTrace();
    }
}


public class TopicSubscriber implements MqttCallback{
public static void main( String[] args )
{
    try {
        MqttClient clientSub = new MqttClient("tcp://iot.eclipse.org:1883", "Subscribing");
        clientSub.connect();
        clientSub.subscribe("MQTT/#", 2);
    } catch (MqttException me) {
        // TODO Auto-generated catch block
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        me.printStackTrace();
    }



}

@Override
public void connectionLost(Throwable arg0) {
    // TODO Auto-generated method stub
    System.out.println("1");
}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
    // TODO Auto-generated method stub
    System.out.println("2");
}

@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
    // TODO Auto-generated method stub
    System.out.println(arg1.getPayload()+"!!!!!!!");

}

}

1 Answers1

1

You implemented MqttCallback but never set it as callback to the MqttClient. One solution would be to add the following:

clientSub.connect();
clientSub.subscribe("MQTT/#", 2);
clientSub.setCallback(new TopicSubscriber());
Thread.sleep(10000);
clientSub.disconnect();

The sleep is necessary because otherwise the applications exits before the callback can be executed.

Another option is to use subscribe with a IMqttMessageListener as parameter. This way you can subscribe to different topics with different handlers.

A good overview (in german) can be found here: https://jaxenter.de/iot-allrounder-27208

Daniel
  • 1,494
  • 9
  • 9