Step By Step
1、设备端建立连接进行消息收发
2、添加pom.xml依赖
3、集成测试
一、设备端建立连接进行消息收发
- 参考官方Demo: MQ4IoTSendMessageToMQ4IoTUseSignatureMode 完整正常的消息收发,确保设备在线。
- 控制台快速查看
二、添加pom.xml依赖
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>onsmqtt20200420</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
三、集成测试
- 3.1 CodeSample
import com.aliyun.onsmqtt20200420.models.*;
import com.aliyun.teaopenapi.models.*;
import com.google.gson.Gson;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.onsmqtt20200420.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
// config.endpoint = "onsmqtt.cn-qingdao.aliyuncs.com";
config.endpoint = "onsmqtt.cn-beijing.aliyuncs.com";
return new com.aliyun.onsmqtt20200420.Client(config);
}
public static void main(String[] args_) throws Exception {java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.onsmqtt20200420.Client client = Sample.createClient("<access key>", "<access secret>");
QuerySessionByClientIdRequest querySessionByClientIdRequest = new QuerySessionByClientIdRequest()
.setClientId("GID_demo1@@@DEVICE_001")
.setInstanceId("post-cn-st*********");
// 复制代码运行请自行打印 API 的返回值
QuerySessionByClientIdResponse response = client.querySessionByClientId(querySessionByClientIdRequest);
// System.out.println(response.body.onlineStatus);
System.out.println(new Gson().toJson(response.body));
}
}
- 3.2 The Result
{"onlineStatus":true,"requestId":"87D17FA5-F1F9-48F3-893C-BD70C8CA3DED"}