|
@@ -1,113 +0,0 @@
|
|
|
-package com.zhouhong.mqtt.emqt.message;
|
|
|
-
|
|
|
-import com.zhouhong.mqtt.emqt.back.MqttSendCallBack;
|
|
|
-import com.zhouhong.mqtt.emqt.config.MqttProperties;
|
|
|
-import lombok.extern.log4j.Log4j2;
|
|
|
-import org.eclipse.paho.client.mqttv3.*;
|
|
|
-import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.UUID;
|
|
|
-
|
|
|
-/**
|
|
|
- * description:
|
|
|
- * date: 2022/6/16 15:54
|
|
|
- *
|
|
|
- * @author: zhouhong
|
|
|
- */
|
|
|
-@Component
|
|
|
-@Log4j2
|
|
|
-public class MqttSend {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private MqttSendCallBack mqttSendCallBack;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private MqttProperties mqttProperties;
|
|
|
-
|
|
|
- public MqttClient connect() {
|
|
|
- return getMqttClient(mqttProperties, mqttSendCallBack);
|
|
|
- }
|
|
|
-
|
|
|
- public static MqttClient getMqttClient(MqttProperties mqttProperties, MqttSendCallBack mqttSendCallBack) {
|
|
|
- MqttClient client = null;
|
|
|
- try {
|
|
|
- String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
|
|
- client = new MqttClient(mqttProperties.getHostUrl(),uuid , new MemoryPersistence());
|
|
|
- MqttConnectOptions options = new MqttConnectOptions();
|
|
|
- options.setUserName(mqttProperties.getUsername());
|
|
|
- options.setPassword(mqttProperties.getPassword().toCharArray());
|
|
|
- options.setConnectionTimeout(mqttProperties.getTimeout());
|
|
|
- options.setKeepAliveInterval(mqttProperties.getKeepAlive());
|
|
|
- options.setCleanSession(true);
|
|
|
- options.setAutomaticReconnect(false);
|
|
|
- try {
|
|
|
- // 设置回调
|
|
|
- client.setCallback(mqttSendCallBack);
|
|
|
- client.connect(options);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return client;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发布消息
|
|
|
- * 主题格式: server:report:$orgCode(参数实际使用机构代码)
|
|
|
- *
|
|
|
- * @param retained 是否保留
|
|
|
- * @param orgCode orgId
|
|
|
- * @param pushMessage 消息体
|
|
|
- */
|
|
|
- public void publish(boolean retained, String orgCode, String pushMessage) {
|
|
|
- MqttMessage message = new MqttMessage();
|
|
|
- message.setQos(mqttProperties.getQos());
|
|
|
- message.setRetained(retained);
|
|
|
- message.setPayload(pushMessage.getBytes());
|
|
|
- MqttDeliveryToken token;
|
|
|
- MqttClient mqttClient = connect();
|
|
|
- try {
|
|
|
- mqttClient.publish("server:report:" + orgCode, message);
|
|
|
- } catch (MqttException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- disconnect(mqttClient);
|
|
|
- close(mqttClient);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 关闭连接
|
|
|
- *
|
|
|
- * @param mqttClient
|
|
|
- */
|
|
|
- public static void disconnect(MqttClient mqttClient) {
|
|
|
- try {
|
|
|
- if (mqttClient != null) {
|
|
|
- mqttClient.disconnect();
|
|
|
- }
|
|
|
- } catch (MqttException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 释放资源
|
|
|
- *
|
|
|
- * @param mqttClient
|
|
|
- */
|
|
|
- public static void close(MqttClient mqttClient) {
|
|
|
- try {
|
|
|
- if (mqttClient != null) {
|
|
|
- mqttClient.close();
|
|
|
- }
|
|
|
- } catch (MqttException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|