首页 > 其他分享 >SpringBoot整合EMQ

SpringBoot整合EMQ

时间:2022-08-29 22:01:49浏览次数:73  
标签:SpringBoot void 连接 client EMQ 整合 new true options

1.引入依赖

<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.5</version>
</dependency>

2.发布消息到EMQ

(1)搭建基本的springBoot程序。

(2)编写controller,新增发布消息方法。

@GetMapping("/publish")
public void publish() throws MqttException {

    MqttClientPersistence persistence = new MemoryPersistence();;//内存持久化
    MqttClient client = new MqttClient("tcp://192.168.200.128:1883", "abc", persistence);

    //连接选项中定义用户名密码和其它配置
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(true);//参数为true表示清除缓存,也就是非持久化订阅者,这个时候只要参数设为true,一定是非持久化订阅者。而参数设为false时,表示服务器保留客户端的连接记录
    options.setAutomaticReconnect(true);//是否自动重连
    options.setConnectionTimeout(30);//连接超时时间  秒
    options.setKeepAliveInterval(10);//连接保持检查周期  秒
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); //版本
    
    client.connect(options);//连接
    client.publish("topic", "发送内容".getBytes(), 2, false);

}

3.订阅消息

在controller新增方法,订阅消息

@GetMapping("/subscribe")
public void subscribe() throws MqttException {

    MqttClientPersistence persistence = new MemoryPersistence();;//内存持久化
    MqttClient client = new MqttClient("tcp://192.168.200.128:1883", "abc", persistence);

    //连接选项中定义用户名密码和其它配置
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(true);//参数为true表示清除缓存,也就是非持久化订阅者,这个时候只要参数设为true,一定是非持久化订阅者。而参数设为false时,表示服务器保留客户端的连接记录
    options.setAutomaticReconnect(true);//是否自动重连
    options.setConnectionTimeout(30);//连接超时时间  秒
    options.setKeepAliveInterval(10);//连接保持检查周期  秒
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); //版本

    client.setCallback(new MqttCallbackExtended() {
        @Override
        public void connectionLost(Throwable throwable) {
            System.out.println("连接丢失!");
        }

        @Override
        public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            System.out.println( "接收到消息  topic:" +s+"  id:"+mqttMessage.getId() +" message:"+ mqttMessage.toString());
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        }

        @Override
        public void connectComplete(boolean b, String s) {
            System.out.println("连接成功!");
        }
    });
    client.connect(options);//连接
    client.subscribe("test");  //订阅主题

}

注:这里可以把发送消息、接收消息的方法封装为Bean来供其它微服务使用

标签:SpringBoot,void,连接,client,EMQ,整合,new,true,options
From: https://www.cnblogs.com/sy2022/p/16637571.html

相关文章

  • springboot1
    1.5.5常用的监控终端在百度搜索“springbootactuator”即可找到如下表格。第2章SpringBoot重要用法2.1自定义异常页面对于404、405、500等异常状态,服务器......
  • SpringBoot+@Async
    1.使用@Async注解使用@Async注解就能简单的将原来的同步函数变为异步(注:@Async所修饰的函数不要定义为static类型,这样异步调用不会生效)@ComponentpublicclassTask{......
  • springboot定时同步数据,从sqlserver到mysql
    定时同步数据,从sqlserver到mysql 注意事项:一.primary:master #设置默认的数据源或者数据源组,默认值即为master二.@Scheduled()和 @DS("slave_1")注解 步骤:1.......
  • jenkins整合docker及harbor的使用
    修改镜镜仓库地址:  systemctlrestartdocker  repo是harbor仓库的仓库名称          修改为3.0并提交         ......
  • springboot---DI入门
                ......
  • springboot知识点总结
    原文地址:https://www.cnblogs.com/lidar/p/15913128.html一、springboot总结1、概述springboot是一种快速使用spring框架的简便方式,springboot简单来说相当于一个程序搭建......
  • SpringBoot整合mybatis
    9、SpringBoot整合mybatisMyBatis-Spring-Boot-Starter官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/maven仓库:https://mvnrepos......
  • Swagger2—整合SpringBoot
    一、Swagger用于生成服务器接口的规范文档的工具,并且能够对接口进行测试的工具1.1作用生成接口说明文档对接口进行测试1.2整合添加依赖(Swagger2、SwaggerUI)......
  • 4.Springboot多配置
    2.Profile2.1Profile多文件我们在主配置文件编写的时候,文件名可以是application-{profifile}.properties/yml默认使用application.properties的配置;2.2yml支持多文......
  • 5.Springboot离线新建环境
    1.新建Maven项目2.pom文件导入org.springframework.bootspring-boot-starter-parent2.7.2org.springframework.bootspring-boot-starter-web<dependency>......