Message Queue for MQTT allows you to perform synchronous queries on or subscribe to asynchronous status notifications of a Message Queue for MQTT client to obtain the status of the client. This topic describes the principles, scenarios, and implementation of the two methods. This topic also describes the differences between the two methods.

Principles

The Message Queue for MQTT broker allows you to use the following methods to obtain the status of a Message Queue for MQTT client:
  • Synchronous query

    This method is relatively simple. You can make an HTTP or HTTPS request by using a public endpoint to call an API operation to query the status of a specific client. You can also use this method to query the statuses of multiple clients at a time.

  • Asynchronous status notification

    This is a notification-based method. If a Message Queue for MQTT client goes online or offline, the Message Queue for MQTT broker sends a notification about the status change event to a backend service application that is deployed on an Alibaba Cloud server.

    In this case, the backend service application is asynchronously notified of the status change event instead of the real-time status of the client. The backend service application must determine the client status based on the timeline of a series of events.

Scenarios

The preceding two methods apply to the following scenarios:

  • Synchronous query
    • The subsequent operation logic depends on whether the specified Message Queue for MQTT client is online during the main service process.
    • O&M engineers need to determine whether the specified Message Queue for MQTT client is online.
  • Asynchronous status notification
    • The Message Queue for MQTT broker needs to trigger specific predefined actions when the specified Message Queue for MQTT client goes online or offline.
    • The Message Queue for MQTT broker needs to collect and analyze the data recorded in the status change events of the specified Message Queue for MQTT client, and sends notifications based on the events.

Differences

The two methods have the following differences:
  • Synchronous queries can be performed to query the real-time status of a specific Message Queue for MQTT client, which is theoretically more accurate than the other method.
  • Asynchronous status notifications are based on message decoupling. This method is more complex and prone to misjudgment when a client status is being determined. However, this method can be used to analyze the status traces of multiple Message Queue for MQTT clients based on events. You can use this method to collect statistics on the statuses of a large number of Message Queue for MQTT clients.

Implementation

  • Synchronous query

    You can call the following operations to query the status of one or more clients:

  • Asynchronous status notification

    You can call the cloud SDK that is provided by Message Queue for MQTT to obtain the status notifications of a Message Queue for MQTT client. For more information about how to download the cloud SDK, see Release notes.

    The following sample code shows how to receive the status notifications of a Message Queue for MQTT client:
    package com.aliyun.openservices.lmq.example;
    
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.mqtt.server.ServerConsumer;
    import com.alibaba.mqtt.server.callback.StatusListener;
    import com.alibaba.mqtt.server.config.ChannelConfig;
    import com.alibaba.mqtt.server.config.ConsumerConfig;
    import com.alibaba.mqtt.server.model.StatusNotice;
    
    public class MQTTClientStatusNoticeProcessDemo {
        public static void main(String[] args) throws Exception {
             /**
             * The endpoint of the Message Queue for MQTT instance that you created. 
             * To obtain the endpoint for accessing the instance by using the cloud SDK, . 
             * Use a domain name instead of an IP address in the endpoint. Otherwise, a broker exception may occur. 
             */
            String domain = "domain";
    
             /**
             * The port that is used by the cloud SDK. The protocol and port that are used by the cloud SDK must match. Set the port number to 5672. 
             */
            int port = 5672;
    
             /**
             * The ID of the Message Queue for MQTT instance that you created. 
             */
            String instanceId = "instanceId";
    
             /**
             * The AccessKey ID that you created in the Alibaba Cloud RAM console for identity authentication. For information about how to obtain your AccessKey ID, see Obtain an AccessKey pair. 
             */
            String accessKey = "accessKey";
    
             /**
             * The AccessKey secret that you created in the Alibaba Cloud RAM console for identity authentication. The AccessKey secret is required only for signature verification. For information about how to obtain your AccessKey secret, see Obtain an AccessKey pair. 
             */
            String secretKey = "secretKey";
    
             /**
             # The group ID that you created in the Message Queue for MQTT console. 
             * */
            String mqttGroupId = "mqttGroupId";
    
            ChannelConfig channelConfig = new ChannelConfig();
            channelConfig.setDomain(domain);
            channelConfig.setPort(port);
            channelConfig.setInstanceId(instanceId);
            channelConfig.setAccessKey(accessKey);
            channelConfig.setSecretKey(secretKey);
    
            ServerConsumer serverConsumer = new ServerConsumer(channelConfig, new ConsumerConfig());
            serverConsumer.start();
            serverConsumer.subscribeStatus(mqttGroupId, new StatusListener() {
                @Override
                public void process(StatusNotice statusNotice) {
                    System.out.println(JSONObject.toJSONString(statusNotice));
                }
            });
        }
    
    }

References

Export online and offline events of Message Queue for MQTT clients