连接HiveMQ代理器实现MQTT协议传输

先下载MQTTX: MQTTX: Your All-in-one MQTT Client Toolbox

使用线上免费的MQTTX BROKER:The Free Global Public MQTT Broker | Try Now | EMQ

打开MQTTX,创建连接,点击NEW SUBSCRIPTION,创建一个主题,这里使用test/topic,在下面Json中填写配置好的主题,点击发送测试OK。订阅者,建立一个主题:

java 复制代码
package com.jasonhong.application.media.mq.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;  
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.MqttMessage;  
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  
  
public class MqttSubscriber implements MqttCallback {  
  
    public static void main(String[] args) {  
        String brokerUrl = "tcp://broker.hivemq.com:1883"; // 使用公共MQTT代理或你的MQTT代理地址
//        String brokerUrl = "tcp://localhost:1883"; // Mosquitto代理地址和端口
        String clientId = "JavaSubscriber";  
        String topic = "test/topic";  
        int qos = 2;  
  
        try (MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence())) {  
            client.setCallback(new MqttSubscriber());  
            MqttConnectOptions connOpts = new MqttConnectOptions();  
            connOpts.setCleanSession(true);  
            client.connect(connOpts);  
            client.subscribe(topic, qos);  
        } catch (MqttException e) {  
            e.printStackTrace();  
        }  
    }  
  
    @Override  
    public void connectionLost(Throwable cause) {  
        System.out.println("Connection lost");  
        cause.printStackTrace();  
    }  
  
    @Override  
    public void messageArrived(String topic, MqttMessage message) throws Exception {  
        System.out.println("Message arrived: " + new String(message.getPayload()));  
    }  
  
    @Override  
    public void deliveryComplete(IMqttDeliveryToken token) {  
        System.out.println("Delivery complete");  
    }  
}

发布者发送给一个消息

java 复制代码
package com.jasonhong.application.media.mq.mqtt;

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.MqttMessage;  
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  
  
public class MqttPublisher {  
  
    public static void main(String[] args) {  
        String brokerUrl = "tcp://broker.hivemq.com:1883"; // 使用公共MQTT代理或你的MQTT代理地址
//        String brokerUrl = "tcp://localhost:1883"; // Mosquitto代理地址和端口
        String clientId = "JavaPublisher";  
        String topic = "test/topic";  
        int qos = 2;  
        String content = "Hello, MQTT!";  
  
        try (MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence())) {  
            MqttConnectOptions connOpts = new MqttConnectOptions();  
            connOpts.setCleanSession(true);  
            client.connect(connOpts);  
            MqttMessage message = new MqttMessage(content.getBytes());  
            message.setQos(qos);  
            client.publish(topic, message);  
            System.out.println("Message published: " + content);  
        } catch (MqttException e) {  
            e.printStackTrace();  
        }  
    }  
}
相关推荐
努力的家伙是不讨厌的几秒前
解析json导出csv或者直接入库
开发语言·python·json
Envyᥫᩣ14 分钟前
C#语言:从入门到精通
开发语言·c#
九圣残炎31 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge33 分钟前
Netty篇(入门编程)
java·linux·服务器
童先生35 分钟前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu37 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
Re.不晚1 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
老秦包你会1 小时前
Qt第三课 ----------容器类控件
开发语言·qt
凤枭香1 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
雷神乐乐1 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven