JAVA实现和AWS IOT订阅、发布MQTT消息
前言
本文使用JAVA实现和AWS IOT传递MQTT消息。
1. AWS控制台,订阅某个主题的消息。JAVA代码向同一主题推送消息。可在AWS控制台查看推送过来的消息。
2. JAVA订阅某个主题的消息。AWS控制台向同一主题推送消息。可在JAVA输出AWS推送过来的消息。
即可实现双向通信。只要订阅的是同一个主题,均可实现消息的通信。
因为用到了AWS服务,故需要使用AWS相关配置。此处参见AWS官网。
记录下本地代码位置:D:\len\iotServer
展示效果
第一张图展示AWS控制台订阅sdk/test/java主题
第二张图展示JAVA推送过来的消息
代码实现
main方法中可通过注释或打开【订阅消息】或【推送消息】结合AWS控制台测试。
也可同时打开,消息会自己推送自己接收,JAVA控制台效果如下。
具体代码
import com.amazonaws.services.iot.client.*; import java.util.UUID; // 推送、订阅消息的主实现 public class ApplicationMain { private static AWSIotMqttClient awsIotClient; // 订阅主题 private static final String TestTopic = "sdk/test/java"; private static final AWSIotQos TestTopicQos = AWSIotQos.QOS0; // AWS连接凭证-需要使用自己的配置 private static final String clientEndpoint = "XXX-ats.iot.cn-northwest-1.amazonaws.com.cn"; private static final String awsAccessKeyId = "XXX"; private static final String awsSecretAccessKey = "XXX"; // 事物ID-需保证唯一 public static String clientId = UUID.randomUUID().toString(); /** * 推送和订阅的主方法 */ public static void main(String[] args) throws AWSIotException, InterruptedException { awsIotClient = initClient(); // 初始化设备连接 awsIotClient.connect(); // 订阅消息 AWSIotTopic topic = new TopicListener(TestTopic, TestTopicQos); awsIotClient.subscribe(topic, true); // 推送消息 Thread publisherThread = new Thread(new PubisherThread(awsIotClient)); publisherThread.start(); publisherThread.join(); } /** * 初始化设备连接 * @return */ public static AWSIotMqttClient initClient() { if (awsAccessKeyId != null && awsSecretAccessKey != null) { awsIotClient = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey); } return awsIotClient; } /** * 循环推送数据 */ public static class PubisherThread implements Runnable { private final AWSIotMqttClient awsIotClient; public PubisherThread(AWSIotMqttClient awsIotClient) { this.awsIotClient = awsIotClient; } @Override public void run() { long counter = 1; while (true) { String payload = "测试:" + (counter++); AWSIotMessage message = new PublishListener(TestTopic, TestTopicQos, payload); try { awsIotClient.publish(message); } catch (AWSIotException e) { System.out.println("推送失败:" + payload); } try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("推送中断"); return; } } } } }
推送消息后的回调类
import com.amazonaws.services.iot.client.AWSIotMessage; import com.amazonaws.services.iot.client.AWSIotQos; /** * 推送消息后回调 */ public class PublishListener extends AWSIotMessage { public PublishListener(String topic, AWSIotQos qos, String payload) { super(topic, qos, payload); } @Override public void onSuccess() { System.out.println("推送成功回调:" + getStringPayload()); } @Override public void onFailure() { System.out.println("推送失败回调:" + getStringPayload()); } @Override public void onTimeout() { System.out.println("推送超时回调:" + getStringPayload()); } }
订阅主题后,接收内容类
import com.amazonaws.services.iot.client.AWSIotMessage; import com.amazonaws.services.iot.client.AWSIotQos; import com.amazonaws.services.iot.client.AWSIotTopic; /** * 订阅主题后,接收内容 */ public class TopicListener extends AWSIotTopic { public TopicListener(String topic, AWSIotQos qos) { super(topic, qos); } @Override public void onMessage(AWSIotMessage message) { System.out.println("接收数据: " + message.getStringPayload()); } }