0

I have set-up MQTT subscription as shown here:

package com.mqttW.demo;
import java.text.SimpleDateFormat;
import java.util.*;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.*;

public class WSync implements MqttCallback {
    private String BROKER_URL = "";
    private String PROTOCOL = "tcp://";
    private String PORT = "1883";
    private String TOPIC_ROOT_UNCNF = "/P/uncnf/";
    private String TOPIC_ROOT_CNF = "/P/cnf/";

    private DbOperations dbo = new DbOperations();

    public void setBrokerUrl() {
        this.BROKER_URL = PROTOCOL + System.getenv("BROKER_DNS") + ":" + PORT;
    }

    public String getBrokerUrl() {
        return this.BROKER_URL;
    }

    public void publishPayload(String wName, String txnType, String payload) {
        String clientId = wName + "-PUB";
        String broker = this.getBrokerUrl();
        String topic = TOPIC_ROOT_UNCNF + txnType + "/" + wName;

        try {
            MqttClient w = new MqttClient(broker, clientId);
            w.connect();

            MqttMessage message = new MqttMessage(payload.getBytes());
            message.setQos(2);
            w.publish(topic, message);

            w.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void processTxns(String wName) {
        this.setBrokerUrl();

        String broker = this.getBrokerUrl();
        String clientId = wName + "-SUB";
        String topic = TOPIC_ROOT_CNF + wName + "/CR";
        MemoryPersistence persistence = new MemoryPersistence();   

        try {
            MqttConnectOptions c = new MqttConnectOptions();
            c.setCleanSession(false);

            MqttClient w = new MqttClient(broker, clientId, persistence);
            w.connect(c);
            w.setCallback(this);
            w.subscribe(topic, 2);
            System.out.println(w.getServerURI() + " " + w.getClientId() + " " + w.isConnected());

        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void connectionLost(Throwable arg0) {
        System.out.println("Connection lost at : " + new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(new java.util.Date()));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String s = new String(message.getPayload());
        System.out.println(s);
    }
}

The class with this method does implement MqttCallback. The System.out.println shows the broker URL, client ID and connected status (true) correctly.

So, what I am not getting is, why does the code terminate? Why is the subscription not set-up to listen for messages?

cogitoergosum
  • 2,309
  • 4
  • 38
  • 62
  • What do you mean by terminate? A call to this method will not block, the client will run in the background. We probably need more context as to what you call around this to be able to answer. – hardillb Feb 18 '17 at 18:42
  • @hardillb The example at the link below waits - and displays all subscribed messages - until I kill it with a `Ctrl-C`. Whereas, in the example I showed, the java process simply exits out. http://stackoverflow.com/a/22716162/919480 – cogitoergosum Feb 18 '17 at 18:47
  • yes, but you've not shown the surrounding code for your example, edit the question so it has the complete class – hardillb Feb 18 '17 at 18:49
  • Edited with complete class. This class is instantiated as a `private` elsewhere and then the `processTxns()` is called. – cogitoergosum Feb 18 '17 at 18:55
  • So, I re-wrote the class with a `public static void main(String[] args)` method, shoved it under another package and ran it. No problems! Perhaps, this was a Java issue than a MQTT one - will take it up some other time. – cogitoergosum Feb 19 '17 at 05:18

0 Answers0