This topic describes the interfaces that are provided by ApsaraMQ for MQTT cloud SDK for Java and the relevant parameters.
Usage notes
- You can configure common parameters and network parameters in the ChannelConfig.class file. The ChannelConfig.class file is stored in the server-sdk-{SDK Version}\com\alibaba\mqtt\server\config path. When you specify the path, replace
{SDK Version}
with the version number of the cloud SDK for Java that you use. For information about how to download the cloud SDK for Java, see Release notes. - In most cases, the default values of the network parameters are used. If you need to change the values, configure the network parameters based on the network status.
Common parameters
Parameter | Description |
---|---|
domain | The endpoint of your ApsaraMQ for MQTT instance. A Message Queue for MQTT client uses an endpoint of a Message Queue for MQTT instance to connect to the ApsaraMQ for MQTT broker. When you use the cloud SDK to connect to ApsaraMQ for MQTT, specify the endpoint in the following format: Important You can use a cloud SDK to connect to only ApsaraMQ for MQTT instances whose kernel version is V3.3.0 and that are deployed in regions in the Chinese mainland.
You can view the ID of the Message Queue for MQTT instance in the Basic Information section of the Instance Details page in the ApsaraMQ for MQTT console. |
port | The port that is used by the cloud SDK for Java. The protocol and port that are used by the cloud SDK must match. Set the value to 5672. |
instanceId | The ID of the instance that you created in the ApsaraMQ for MQTT console. You can view the instance ID on the Instance Details page of the ApsaraMQ for MQTT console. |
accessKey | The AccessKey ID that you created in the Resource Access Management (RAM) console for identity authentication. For information about how to obtain your AccessKey ID, see Obtain an AccessKey pair. |
secretKey | The AccessKey secret that you created in the RAM console for identity authentication. For information about how to obtain your AccessKey secret, see Obtain an AccessKey pair. The AccessKey secret is required only if the signature authentication mode is used. |
Network parameters
Parameter | Description |
---|---|
networkRecoveryInterval | The amount of time that is required for a TCP connection to be automatically recovered. Unit: milliseconds. Default value: 5000. |
connectionTimeout | The timeout period of a request that is sent to establish a TCP connection. Unit: milliseconds. Default value: 60000. If a TCP connection fails to be established within the specified timeout period, an error response is returned. |
handshakeTimeout | The timeout period for a TCP handshake. Unit: milliseconds. Default value: 10000. If a TCP handshake fails to be performed within the specified timeout period, an error response is returned. |
requestedHeartbeat | The heartbeat interval. Units: seconds. Default value: 60. If no heartbeat information is sent within the specified interval, an error response is returned. |
shutdownTimeout | The period during which a consumer can still process messages after a TCP connection is closed. Unit: milliseconds. Default value: 10000. If the parameter is set to 0, a consumer can process messages for an indefinite period of time after the TCP connection is closed.
|
Send messages
- Interface name: sendMessage
- Class name:
com.alibaba.mqtt.server.ServerProducer
Parameter | Description |
---|---|
mqttTopic | The ApsaraMQ for MQTT topic to which messages are sent. You can specify a parent topic or a subtopic. If you specify a subtopic, messages are sent to the subtopic. You must create parent topics in the ApsaraMQ for MQTT console. You can specify subtopics in the code without the need to create them in the console. For more information, see Terms. Important If you specify a topic that does not exist or on which you are not granted permissions, the Message Queue for MQTT client fails to be authenticated. As a result, the client is disconnected from the ApsaraMQ for MQTT broker. |
payload | The body of the message. |
sendCallback | The callback that is invoked when a message is sent. The message status is processed in the callback. |
Subscribe to messages
- Interface name: subscribeTopic
- Class name:
com.alibaba.mqtt.server.ServerConsumer
Parameter | Description |
---|---|
firstTopic | The parent topic that you created in the ApsaraMQ for MQTT console. For more information, see Terms. Important
|
messageListener | The callback that is invoked when a message is subscribed to. The message is processed in the callback. |
Subscribe to client status notifications
- Interface name: subscribeStatus
- Class name:
com.alibaba.mqtt.server.ServerConsumer
Parameter | Description |
---|---|
mqttGroupId | The ID of the group that you created in the ApsaraMQ for MQTT console. For more information, see Terms. |
statusListener | The callback that is invoked when a client status notification is subscribed to. The client status notification is processed in the callback. |