Java中mqtt消息队列发送和订阅消息

本文详细介绍如何使用Java实现MQTT协议,包括本地服务器搭建、代码示例、依赖包引入等关键步骤。通过实例演示了如何创建客户端、连接服务器、发布和订阅消息,以及设置回调方法处理接收的消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先本地建立mqtt协议的服务器

2.直接上代码

package io.test;

import java.util.Date;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
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 mqtt3 {

	public static void main(String[] args) throws MqttException {
		// 本地mq服务器
		String THINGSBOARD_HOST = "tcp://localhost:1883";
		//标识mq发送的客户端id
		String PUBLISH_CLIENTID = "clientId";
		//发送到服务器的topic标识
		String PUBLISH_TOPIC = "v1/devices/me/telemetry";
		//订阅到服务器的topic标识
		String SUBSCRIBE_TOPIC = "v1/devices/me/rpc/request/+";
		//mqtt连接
		MqttClient mqttClient = new MqttClient(THINGSBOARD_HOST, PUBLISH_CLIENTID, new MemoryPersistence());
		//设置超时时间
		mqttClient.setTimeToWait(10000);
		//进行连接
		mqttClient.connect(getOptions());
		//订阅
		mqttClient.subscribe(SUBSCRIBE_TOPIC, 2);
		//回调方法
		mqttClient.setCallback(new MqttCallbackExtended() {
			@Override
			public void messageArrived(String topic, MqttMessage message) throws Exception {
				String context = new String(message.getPayload());
				System.out.println(context);
			}
			
			@Override
			public void deliveryComplete(IMqttDeliveryToken token) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void connectionLost(Throwable cause) {
				// TODO Auto-generated method stub
				System.out.println("connect  lost");
				
			}
			
			@Override
			public void connectComplete(boolean reconnect, String serverURI) {
				// TODO Auto-generated method stub
				System.out.println("connect success-------");
			}
			
		});
		
		
		
		
		String sendCon="{\"ts\":"+new Date().getTime()+", \"values\":{\"test\":1111}}";
		mqttClient.publish(PUBLISH_TOPIC, sendCon.getBytes(), 0, false);
		System.out.println("发送成功:"+sendCon);
		
		

	}

	//连接到mqtt的连接参数配置
	public static MqttConnectOptions getOptions() {
	    MqttConnectOptions options = new MqttConnectOptions();
	    //设置session是否保留上一条记录
	    options.setCleanSession(false);
	    //连接超时时间
	    options.setConnectionTimeout(10);
	    //心跳会话时间
	    options.setKeepAliveInterval(60);
	    //自动重连
	    options.setAutomaticReconnect(true);
	    return options;
	}
	
}

3.我是用的mqtt3 直接引入包:

 <!-- https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3 -->
		<dependency>
		    <groupId>org.eclipse.paho</groupId>
		    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
		    <version>1.2.0</version>
		</dependency>

 

转载于:https://my.oschina.net/liketome/blog/1856738

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值