JAVA使用搭建EMQX消息队列

简介

本文主要介绍怎么用java客户端paho连接emqx并实现共享订阅,所谓共享订阅就是在开多个节点的客户端消费时,保证一条消息有且仅有一个节点消费,不会造成重复消费。

1.依赖导入

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

2.写入配置文件和配置类

server:
    port: 8082
spring:
  mqtt:
    username: admin							# 账号
    password: public						# 密码
    host-url: tcp://127.0.0.1:1883	# mqtt连接tcp地址
    client-id: mq-dky-0813		# 客户端Id,每个启动的id要不同
    default-topic: topic-test		# 默认主题
    timeout: 100							# 超时时间
    keepalive: 100

配置类:

package com.lesso.collect_agent.mqtt;
 
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName: MqttConfig
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/16 14:43:39 
 * @Version: V1.0
 **/
@Component
@ConfigurationProperties("spring.mqtt")
@Setter
@Getter
public class MqttConfig {
    @Autowired
    private MqttPushClient mqttPushClient;
 
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 连接地址
     */
    private String hostUrl;
    /**
     * 客户Id
     */
    private String clientId;
    /**
     * 默认连接话题
     */
    private String defaultTopic;
    /**
     * 超时时间
     */
    private int timeout;
    /**
     * 保持连接数
     */
    private int keepalive;
 
    @Bean
    public MqttPushClient getMqttPushClient() {
        mqttPushClient.connect(hostUrl, clientId, username, password, timeout, keepalive,defaultTopic);
        // 以/#结尾表示订阅所有以test开头的主题
        mqttPushClient.subscribe(defaultTopic, 0);
        return mqttPushClient;
    }
}

3.连接broker核心类和消息发布回调类

package com.lesso.collect_agent.mqtt;
 
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName: MqttPushClient
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/16 14:48:38 
 * @Version: V1.0
 **/
@Component
public class MqttPushClient {
    private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
 
    private static MqttClient client;
 
    private static MqttClient getClient() {
        return client;
    }
 
    private static void setClient(MqttClient client) {
        MqttPushClient.client = client;
    }
 
    /**
     * 客户端连接
     *
     * @param host      ip+端口
     * @param clientID  客户端Id
     * @param username  用户名
     * @param password  密码
     * @param timeout   超时时间
     * @param keepalive 保留数
     */
    public void connect(String host, String clientID, String username, String password, int timeout, int keepalive,String mqttTopic) {
        MqttClient client;
        try {
            client = new MqttClient(host, clientID, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepalive);
            // 设置自动重连
            options.setAutomaticReconnect(true);
            MqttPushClient.setClient(client);
            try {
                // 此处使用的MqttCallbackExtended类而不是MqttCallback,是因为如果emq服务出现异常导致客户端断开连接后,重连后会自动调用connectComplete方法
                client.setCallback(new MqttCallbackExtended() {
                    @Override
                    public void connectComplete(boolean reconnect, String serverURI) {
                        System.out.println("连接完成...");
                        try {
                            // 重连后要自己重新订阅topic,这样emq服务发的消息才能重新接收到,不然的话,断开后客户端只是重新连接了服务,并没有自动订阅,导致接收不到消息
                            client.subscribe(mqttTopic);
                            logger.info("订阅成功");
                        }catch (Exception e){
                            logger.info("订阅出现异常:{}", e);
                        }
                    }

                    @Override
                    public void connectionLost(Throwable cause) {
                        System.out.println("失去连接....");
                    }

                    @Override
                    public void messageArrived(String topic, MqttMessage message) throws Exception {
                        // subscribe后得到的消息会执行到这里面
                        String content = new String(message.getPayload());
                        System.out.println("接收消息主题 : " + topic);
                        System.out.println("接收消息Qos : " + message.getQos());
                        System.out.println("接收消息内容 : " + content);
                        // 处理数据
                        // messageConsumeService.dealEmqContent(content);
                    }

                    @Override
                    public void deliveryComplete(IMqttDeliveryToken token) {
                        System.out.println("deliveryComplete....");
                    }
                });
                client.connect(options);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 发布
     *
     * @param qos         连接方式
     * @param retained    是否保留
     * @param topic       主题
     * @param pushMessage 消息体
     */
    public void publish(int qos, boolean retained, String topic, String pushMessage) {
        MqttMessage message = new MqttMessage();
        message.setQos(qos);
        message.setRetained(retained);
        message.setPayload(pushMessage.getBytes());
        MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic);
        if (null == mTopic) {
            logger.error("topic not exist");
        }
        MqttDeliveryToken token;
        try {
            token = mTopic.publish(message);
            token.waitForCompletion();
        } catch (MqttPersistenceException e) {
            e.printStackTrace();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 订阅某个主题
     *
     * @param topic 主题
     * @param qos   连接方式
     */
    public void subscribe(String topic, int qos) {
        logger.info("==============开始订阅主题=========" + topic);
        try {
            MqttPushClient.getClient().subscribe(topic, qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

4.HTTP调用测试发布消息

package com.lesso.collect_agent.web;
 
import com.lesso.collect_agent.mqtt.MqttPushClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName: PullController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/16 14:56:18 
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/")
public class PullController {
    @Autowired
    private MqttPushClient mqttPushClient;
    /**
    * @author liujianfu
    * @description    测试发布主题
    * @date 2021/8/16 15:04
    * @param sendMessage
    * @return RUtils
    */
    @GetMapping(value = "/publishTopic")
    public String publishTopic(String sendMessage) {
       System.out.println("message:"+sendMessage);
       sendMessage=sendMessage+" : {\"name\":\"ljf\",\"age\":345}";
        mqttPushClient.publish(0,false,"topic-test",sendMessage);
        return "OK";
    }
}
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值