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

ParameterDescription
domainThe 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.
  • Public Endpoint: ID of the ApsaraMQ for MQTT instance-server-internet.mqtt.aliyuncs.com
  • VPC Endpoint: ID of the ApsaraMQ for MQTT instance-server-internal.mqtt.aliyuncs.com

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.

portThe 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.
instanceIdThe 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.
accessKeyThe 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.
secretKeyThe 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

ParameterDescription
networkRecoveryIntervalThe amount of time that is required for a TCP connection to be automatically recovered. Unit: milliseconds. Default value: 5000.
connectionTimeoutThe 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.

handshakeTimeoutThe 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.

requestedHeartbeatThe heartbeat interval. Units: seconds. Default value: 60.

If no heartbeat information is sent within the specified interval, an error response is returned.

shutdownTimeoutThe 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
Table 1. Parameters
ParameterDescription
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.
payloadThe body of the message.
sendCallbackThe 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
Table 2. Parameters
ParameterDescription
firstTopicThe parent topic that you created in the ApsaraMQ for MQTT 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.
  • In most cases, you use the cloud SDK to subscribe to messages in scenarios in which cloud applications collect and analyze messages. As a result, you cannot specify subtopics when you use the cloud SDK to subscribe to messages.
messageListenerThe 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
Table 3. Parameters
ParameterDescription
mqttGroupId

The ID of the group that you created in the ApsaraMQ for MQTT console. For more information, see Terms.

statusListenerThe callback that is invoked when a client status notification is subscribed to. The client status notification is processed in the callback.