1. To use the service-side subscription function, you need to set it on the console and select the type of message you want to push.
2. Adding dependencies to the Maven project, as shown below.
<dependencies> <!-- Aliyun core --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.3.5</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-iot</artifactId> <version>6.11.0</version> </dependency> <!-- iot message client --> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>iot-client-message</artifactId> <version>1.1.3</version> </dependency> <!-- Journal --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies>
3. Initialize client with accessKey, accessSecret, regionId and uid of their own account.
// Aliyun accessKey String accessKey = "xxxxxxxxxxxxxxxx"; // Aliyun Access Secret String accessSecret = "xxxxxxxxxxxxxxxxxx"; // regionId String regionId = "cn-shanghai"; // Ali cloud uid String uid = "xxxxxxxxxxxxxxx"; // endPoint: https://${uid}.iot-as-http2.${region}.aliyuncs.com String endPoint = "https://" + uid + ".iot-as-http2." + regionId + ".aliyuncs.com"; // Connection configuration Profile profile = Profile.getAccessKeyProfile(endPoint, regionId, accessKey, accessSecret); // Constructing Client MessageClient client = MessageClientFactory.messageClient(profile);
4. Setting up message receiving interface.
It should be noted that at present, the server subscription can not filter messages, that is to say, the messages of all devices under all products under the account will be subscribed to (provided that the message type to be pushed is set in the first step).
But you can do separate business processing for messages posted by a specific Topic (as follows: message Callback), and other messages without special callbacks, using a common interface (that is, message Callback Comm below).
MessageCallback messageCallbackComm = new MessageCallback() { @Override public Action consume(MessageToken messageToken) { Message m = messageToken.getMessage(); System.out.println("This is common callback!"); System.out.println("topic : " + m.getTopic()); System.out.println("receive : " + new String(m.getPayload())); System.out.println(" "); return MessageCallback.Action.CommitSuccess; } }; MessageCallback messageCallback = new MessageCallback() { @Override public Action consume(MessageToken messageToken) { Message m = messageToken.getMessage(); System.out.println("topic : " + m.getTopic()); System.out.println("receive : " + new String(m.getPayload())); return MessageCallback.Action.CommitSuccess; } }; String topic = "xxxxxxxxxxxxxxxx"; client.setMessageListener(topic,messageCallback); client.connect(messageCallbackComm);
5. Examples.
Suppose the top in the code is set to the Topic (/ sys/${productKey}/${deviceName}/thing/event/property/post) reported by the device property.
Then the device performs the operation of reporting attributes, and the message received on the server side is the content printed in the message Callback.
For other behavior of the device (e.g. offline and offline), the message received at the server is printed in message CallbackComm (that is, This is common callback!