|
@@ -0,0 +1,125 @@
|
|
|
+package com.huaxia.imes.mqtt;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eclipse.paho.client.mqttv3.*;
|
|
|
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zx
|
|
|
+ * @since 2024年11月11日
|
|
|
+ * MQTT 消息生产者
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class MqttProducer {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String borketUrl = "tcp://117.72.113.54:1883";
|
|
|
+ String clientId = "demo_client"+System.currentTimeMillis();
|
|
|
+ String subTopic = "TestTopic";
|
|
|
+ int Qos = 1;
|
|
|
+ String userName = "admin";
|
|
|
+ String password = "public";
|
|
|
+ // MQTT 连接选项设置连接参数
|
|
|
+ MqttConnectOptions options = buildMqttConnectOptions(userName, password);
|
|
|
+ // 内存存储
|
|
|
+ MemoryPersistence persistence = new MemoryPersistence();
|
|
|
+ try {
|
|
|
+ // 创建客户端
|
|
|
+ MqttClient client = new MqttClient(borketUrl, clientId, persistence);
|
|
|
+ // 设置超时时间
|
|
|
+ client.setTimeToWait(5000);
|
|
|
+ // 设置回调
|
|
|
+ client.setCallback(new MqttCallbackExtended() {
|
|
|
+ // 连接断开
|
|
|
+ @Override
|
|
|
+ public void connectionLost(Throwable throwable) {
|
|
|
+ log.error("连接断开");
|
|
|
+ try {
|
|
|
+ log.info("连接断开,正在重连>>>>>>");
|
|
|
+ Thread.sleep(5000);
|
|
|
+ client.connect(options);
|
|
|
+ log.info("重连成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("连接断开异常{}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 消息到达
|
|
|
+ @Override
|
|
|
+ public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
|
|
+ log.info("发送消息内容:" + new String(mqttMessage.getPayload()));
|
|
|
+ log.info("消息主题:" + s);
|
|
|
+ log.info("消息质量:" + mqttMessage.getQos());
|
|
|
+ log.info("消息是否重复:" + mqttMessage.isDuplicate());
|
|
|
+ log.info("消息是否已发布:" + mqttMessage.isRetained());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发布完成
|
|
|
+ @Override
|
|
|
+ public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
|
|
+ log.info("发布完成客户端信息:{}",iMqttDeliveryToken.getClient().getClientId());
|
|
|
+ }
|
|
|
+ // 连接完成
|
|
|
+ @Override
|
|
|
+ public void connectComplete(boolean b, String s) {
|
|
|
+ log.info("连接完成{}",s);
|
|
|
+ log.info("b{}",b);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 连接
|
|
|
+ client.connect(options);
|
|
|
+ // 订阅主题
|
|
|
+ client.subscribe(subTopic);
|
|
|
+ //循环发送消息
|
|
|
+ boolean keepSending = true;
|
|
|
+ int i = 0;
|
|
|
+ while (keepSending) {
|
|
|
+ // 控制发送次数
|
|
|
+ i++;
|
|
|
+ // 发布消息
|
|
|
+ MqttMessage message = new MqttMessage(("第" + i + "只杨羊").getBytes());
|
|
|
+ // 设置消息质量
|
|
|
+ message.setQos(Qos);
|
|
|
+ // 发布消息
|
|
|
+ client.publish(subTopic, message);
|
|
|
+ // 控制发送频率
|
|
|
+ Thread.sleep(1000); // 每秒发送一次
|
|
|
+ if (i >= 1000) { // 发送 10 次后停止
|
|
|
+ keepSending = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 断开连接
|
|
|
+ client.disconnect();
|
|
|
+ // 关闭客户端
|
|
|
+ client.close();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("连接异常{}", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建连接参数
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * @param password
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static MqttConnectOptions buildMqttConnectOptions(String userName, String password) {
|
|
|
+ MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
|
+ // 连接 MQTT Broker 的用户名密码
|
|
|
+ connOpts.setUserName(userName);
|
|
|
+ connOpts.setPassword(password.toCharArray());
|
|
|
+ // 设置是否清除会话
|
|
|
+ connOpts.setCleanSession(true);
|
|
|
+ //心跳间隔
|
|
|
+ connOpts.setKeepAliveInterval(60);
|
|
|
+ // 连接超时时间,单位为秒
|
|
|
+ connOpts.setConnectionTimeout(30);
|
|
|
+ // 是否自动重连
|
|
|
+ connOpts.setAutomaticReconnect(true);
|
|
|
+ return connOpts;
|
|
|
+ }
|
|
|
+}
|