mqtt 3.0 消息推送和消费

如果大家觉得 项目中只能用3.0版本去写mqtt 的发送和消费,那我在这里也提供了3.0版本的代码实例。

首先依赖3.0版本的 maven 

	<dependency>
			<groupId>org.eclipse.paho</groupId>
			<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
			<version>1.2.5</version> <!-- 使用适当的版本 -->
		</dependency>

其他工具类maven依赖

	<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.23</version>
		</dependency>

		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.8.12</version>
		</dependency>

配置连接类 


import com.david.analysis.service.IotDataService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@RequiredArgsConstructor
public class MqttConsumerConfig {
    @Value("${spring.mqtt3.username}")
    private String username;

    @Value("${spring.mqtt3.password}")
    private String password;

    @Value("${spring.mqtt3.url}")
    private String hostUrl;

    @Value("${spring.mqtt3.client.id}")
    private String clientId;

    @Value("${spring.mqtt3.default.topic}")
    private String defaultTopic;

    /**
     * 客户端对象
     */
    private MqttClient client;

    private final IotDataService iotDataService;


    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 在bean初始化后连接到服务器
     */
    @PostConstruct
    public void init() {
        //如果需要用到 就打开下面的注释 mqtt3 和mqtt5 只需要用到一个接收协议 打开这个就需要关闭mqtt5的 启动连接
       // connect();
    }

    /**
     * 客户端连接服务端
     */
    public void connect() {
        try {
            //创建MQTT客户端对象
            client = new MqttClient(hostUrl, clientId, new MemoryPersistence());
            //连接设置
            MqttConnectOptions options = new MqttConnectOptions();
            //是否清空session,设置为false表示服务器会保留客户端的连接记录,客户端重连之后能获取到服务器在客户端断开连接期间推送的消息
            //设置为true表示每次连接到服务端都是以新的身份
            options.setCleanSession(false);
            //设置连接用户名
            options.setUserName(username);
            //设置连接密码
            options.setPassword(password.toCharArray());
            //设置超时时间,单位为秒
            options.setConnectionTimeout(100);
            //设置心跳时间 单位为秒,表示服务器每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线
            options.setKeepAliveInterval(20);
            //设置遗嘱消息的话题,若客户端和服务器之间的连接意外断开,服务器将发布客户端的遗嘱信息
            options.setWill("willTopic", (clientId + "与服务器断开连接").getBytes(), 0, false);
            //设置回调
            client.setCallback(new MqttConsumerCallBack(iotDataService, redisTemplate));
            client.connect(options);
            //订阅主题
            //消息等级,和主题数组一一对应,服务端将按照指定等级给订阅了主题的客户端推送消息
            int[] qos = {1};
            //主题
            String[] topics = {defaultTopic};
            //订阅主题
            client.subscribe(topics, qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    /**
     * 断开连接
     */
    public void disConnect() {
        try {
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }


    /**
     * 订阅主题
     */
    public void subscribe(String topic, int qos) {
        try {
            client.subscribe(topic, qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

配置:

  #MQTT3配置信息
  mqtt3:
    #MQTT服务端地址,端口默认为11883,如果有多个,用逗号隔开
    url: tcp://xxx:1883
    #用户名
    username: xxx
    #密码
    password: xxx
    #客户端id(不能重复)
    client:
      id: consumer-id
    #MQTT默认的消息推送主题,实际可在调用接口时指定
    default:
      topic: xxx/data

消息回调类:

 

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Component
@Slf4j
public class MqttConsumerCallBack implements MqttCallback {


//业务数据库持久化类 根据自己情况调整
    private final IotDataService iotDataService;

    private final RedisTemplate<String, Object> redisTemplate;
   

    public MqttConsumerCallBack(IotDataService iotDataService, RedisTemplate<String, Object> redisTemplate) {
        this.iotDataService = iotDataService;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 客户端断开连接的回调
     */
    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("与服务器断开连接,可重连");
    }


    /**
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println(String.format("接收消息主题 : %s", topic));
        System.out.println(String.format("接收消息Qos : %d", message.getQos()));
        System.out.println(String.format("接收消息内容 : %s", new String(message.getPayload())));
        System.out.println(String.format("接收消息retained : %b", message.isRetained()));

//        String jsonString =  new String(message.getPayload());
//拿到了接收到的消息
        String jsonString = new String(message.getPayload(), StandardCharsets.UTF_8);
            
//业务处理......
        }
    }

    /**
     * 消息发布成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        System.out.println(String.format("接收消息成功"));
    }
}

发送mqtt

 

import com.alibaba.fastjson.JSONObject;
 
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @author: zqb
 * @date: 2024/9/30 16:06
 */
public class Mqtt3SendTest {
    private static final String MQTT_BROKER = "tcp://xxx:1883"; // MQTT服务器地址
    private static final String MQTT_TOPIC = "pyx/data"; //  MQTT主题

    private static IMqttClient mqttClient;
    private static final String USERNAME = "example_user"; // 替换为你的用户名
    private static final String PASSWORD = "xxxx"; // 替换为你的密码
    
    public static void main(String[] args) {
        //接收到设备的原始数据
        String receivedHex="";
        //开始解析 接收到的报文  
       .....
        String babyIncubatorYP970Str="拿到解析后的明文字符串";
        System.out.println("解析结果:"+babyIncubatorYP970Str);

        // 发送数据到MQTT服务器
        sendToMQTT(babyIncubatorYP970Str);
    }

    private static void sendToMQTT(String message) {
        try {
            MqttMessage mqttMessage = new MqttMessage(message.getBytes());
            mqttClient.publish(MQTT_TOPIC, mqttMessage);
            System.out.println("Published to MQTT topic: " + MQTT_TOPIC);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

这样mqtt 3.0版本的完整实例就写完了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值