ApsaraMQ for MQTT allows you to perform synchronous queries or subscribe to asynchronous status notifications to obtain the status of an ApsaraMQ for MQTT client. This topic describes the working mechanism, scenarios, and implementation methods of the two methods. This topic also describes the differences between the two methods.
Working mechanism
The ApsaraMQ for MQTT broker allows you to use the following methods to obtain the status of an ApsaraMQ for MQTT client:
Synchronous queries
This method is relatively simple. You can initiate 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 status of multiple clients at a time.
Asynchronous status notifications
This is a notification-based method. If an ApsaraMQ for MQTT client goes online or offline, the ApsaraMQ for MQTT broker sends a notification about the status change event to a backend service application that is deployed on an Alibaba Cloud server.
This method is an asynchronous process and does not provide real-time information. An application can determine the actual status of a client only based on the timeline of event notifications.
Scenarios
The preceding two methods are suitable for the following scenarios:
Synchronous queries
The subsequent operation logic depends on whether a specific ApsaraMQ for MQTT client is online during the main service process.
O&M engineers must determine whether a specific ApsaraMQ for MQTT client is online.
Asynchronous status notifications
The ApsaraMQ for MQTT broker must trigger specific predefined actions when a specific ApsaraMQ for MQTT client goes online or offline.
The ApsaraMQ for MQTT broker must collect and analyze data that is recorded in the status change events of a specific ApsaraMQ for MQTT client, and sends notifications based on the events.
Differences between synchronous queries and asynchronous status notifications
The following items describe the differences between the two methods:
Synchronous queries can be performed to query the real-time status of a specific ApsaraMQ for MQTT client. This method is theoretically more accurate compared with asynchronous status notifications.
Compared with synchronous queries, asynchronous status notifications are more complicated and less accurate in determining the actual status of an ApsaraMQ for MQTT client because message decoupling is used. However, this method can be used to analyze the status traces of multiple ApsaraMQ for MQTT clients. You can use this method to collect statistics about the status of a large number of ApsaraMQ for MQTT clients.
Implementation methods
Synchronous queries
You can call the following operations to query the status of one or more clients:
Asynchronous status notifications
You can call the cloud SDK that is provided by ApsaraMQ for MQTT to obtain the status notifications of an ApsaraMQ for MQTT client. For information about how to download the cloud SDK, see Release notes.
The following sample code provides an example on how to obtain the status notifications of an ApsaraMQ 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 that is used to access the instance by using the cloud SDK, contact technical support.. * Use a domain name instead of an IP address as 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 value 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 an 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 when the signature authentication mode is used. For information about how to obtain an AccessKey secret, see Obtain an AccessKey pair. */ String secretKey = "secretKey"; /** * The ID of the group 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)); } }); } }