简体   繁体   中英

ActiveMQ + MQTT + subscribe to “ActiveMQ.Advisory.Connection”

This is the context:

A Java application subscribes to the Topic "ActiveMQ.Advisory.Connection" from an ActiveMQ 5.9.1 via MQTT (Paho 0.4.0):

public class SupervisorMqttClient implements MqttCallback {

    private MqttClient client = null;
    private MemoryPersistence persistence = null;
    private MqttConnectOptions connOpts = null;

    private final int STATUS_OK = 0;
    private final int STATUS_ERROR = 1;

    private String mqttServer = null;
    private String clientId = null;
    private int status = STATUS_OK;

    public SupervisorMqttClient() {
        try {
            this.init();
        } catch (MqttException e) {
            Logger.error(e.getLocalizedMessage());
            Logger.debug(e);
        }
    }

    private void init() throws MqttException {
        Properties props = PropertiesManager.getInstance("supervisor");

        mqttServer = props.getProperty("supervisor.mqtt.server");
        String supervisorID = props.getProperty("supervisor.mqtt.client.number");
        clientId = Supervisor.APP_NAME+"-"+supervisorID;

        connOpts = new MqttConnectOptions();
        connOpts.setKeepAliveInterval(30);
        connOpts.setCleanSession(true); // important non-durable

        persistence = new MemoryPersistence();

        client = new MqttClient(mqttServer, clientId, persistence);
        connectAndSubscribe();
    }

    private void connectAndSubscribe() throws MqttSecurityException, MqttException {
        try {
            client.connect(connOpts);
            client.setCallback(this);
            client.subscribe("ActiveMQ/Advisory/Connection");
        } catch (MqttSecurityException e) {
            Logger.error(e.getLocalizedMessage());
            Logger.debug(e);
        } catch (MqttException e) {
            Logger.error(e.getLocalizedMessage());
            Logger.debug(e);
            processError(e);
        }
    }

    public void publish(String orderType, JSONObject jsonExtraData) {
        if (status == STATUS_ERROR) {
            connectAndSubscribe();
        }
        if (status == STATUS_OK) {
            // some code here
        }
    }

    @Override
    public void connectionLost(Throwable err) {
        Logger.info("Connection lost");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken arg0) {
        Logger.info("deliveryComplete");
    }

    @Override
    public void messageArrived(String topic, MqttMessage msg) throws Exception {
        System.out.println("MQTT Mesage Arrived[" + topic + "] Msg[" + msg.toString() + "]");
    }

    private void processError(MqttException e) {
        status = STATUS_ERROR;
        try {
            if (client.isConnected()) {
                Logger.error("disconnecting");
                client.disconnect();
            }
        } catch (MqttException ex) {
            Logger.error(ex.getLocalizedMessage());
            Logger.debug(ex);
        }
    }
}

The connection with ActiveMQ is established fine. This topic offers information about the connections (open/close) in the ActiveMQ, but my problem is that messages I catch are empty:

MQTT Mesage Arrived[ActiveMQ/Advisory/Connection] Msg[]

Is there any way to catch them using MQTT? or I should use JMS for that?

Thanks, Jon Ander.

The question would be what do you want the MQTT client to receive on the Advisory topic as the message body. The advisories generally include much of the information as message properties however those cannot be mapped to MQTT as MQTT messages don't have properties. The body of the Connection advisory is a copy of the ConnectionInfo object that was used to create the connection. On the MQTT side there is not much you could do with that as all you would receive would be the serialized bytes of that object which you wouldn't be able to do anything with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM