ActiveMQ + MQTT +订阅“ActiveMQ.Advisory.Connection”

时间:2014-12-15 14:25:20

标签: java jms activemq mqtt paho

这是上下文:

Java应用程序订阅主题" ActiveMQ.Advisory.Connection"从ActiveMQ 5.9.1到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);
        }
    }
}

与ActiveMQ的连接建立良好。本主题提供有关ActiveMQ中连接(打开/关闭)的信息,但我的问题是我捕获的消息是空的:

  

MQTT Mesage到达[ActiveMQ / Advisory / Connection] Msg []

有没有办法用MQTT捕获它们?或者我应该使用JMS?

谢谢, Jon Ander。

1 个答案:

答案 0 :(得分:1)

问题是您希望MQTT客户端在作为消息正文的咨询主题上接收什么。这些建议通常包含大部分信息作为消息属性,但是由于MQTT消息不具有属性,因此无法将这些信息映射到MQTT。 Connection Advisory的主体是用于创建连接的ConnectionInfo对象的副本。在MQTT方面,你可以做的并不多,因为所有你会收到的是该对象的序列化字节,你将无法做任何事情。