<!--集成MQTT-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
</dependency>
package com.fuel.core.mqtt;
import com.fuel.common.constant.MqttConstants;
import com.fuel.common.core.redis.RedisCache;
import com.fuel.common.utils.StringUtils;
import com.fuel.common.utils.ip.IpUtils;
import com.fuel.common.utils.spring.SpringUtils;
import com.fuel.equipment.domain.FuelDevice;
import com.fuel.equipment.service.IFuelDeviceService;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.List;
@Configuration
public class MqttConsumer {
private static final Logger logger = LoggerFactory.getLogger(MqttConsumer.class);
@Autowired
private MqttMessageListener mqttMessageListener;
@Autowired
private IFuelDeviceService fuelDeviceService;
@Value("${aliMqtt.username}")
private String username;
@Value("${aliMqtt.password}")
private String password;
@Value("${aliMqtt.hostUrl}")
private String hostUrl;
@Value("${aliMqtt.clientId}")
private String clientId;
/**
* 客户端对象
*/
private MqttClient client;
/**
* 在bean初始化后连接到服务器
*/
@PostConstruct
public void init(){
connect();
}
/**
* 客户端连接服务端
*/
public void connect(){
try {
//创建MQTT客户端对象
MqttClient mqttClient = new MqttClient(hostUrl, clientId + System.currentTimeMillis() + "_" + IpUtils.getHostIp(), new MemoryPersistence());
client = mqttClient;
//设置回调
client.setCallback(new MqttConsumerCallBack());
//连接设置
MqttConnectOptions options = new MqttConnectOptions();
//是否清空session,设置为false表示服务器会保留客户端的连接记录,客户端重连之后能获取到服务器在客户端断开连接期间推送的消息
//设置为true表示每次连接到服务端都是以新的身份
options.setCleanSession(true);
options.setAutomaticReconnect(true);// 自动重连
//设置连接用户名
// options.setUserName(username);
//设置连接密码
// options.setPassword(password.toCharArray());
//设置超时时间,单位为秒
options.setConnectionTimeout(100);
//设置心跳时间 单位为秒,表示服务器每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线
options.setKeepAliveInterval(20);
// client.connect(options);
IMqttToken iMqttToken = client.connectWithResult(options);
boolean complete = iMqttToken.isComplete();
logger.info("MQTT连接"+(complete?"成功":"失败"));
RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
List<FuelDevice> fuelDevicesList = redisCache.getCacheObject(MqttConstants.MQTT_PUBLIC_KEY + "fuel_devices_list");
if (StringUtils.isEmpty(fuelDevicesList)) {
fuelDevicesList = fuelDeviceService.selectDeviceListByMqtt(new FuelDevice());
redisCache.setCacheObject(MqttConstants.MQTT_PUBLIC_KEY + "fuel_devices_list", fuelDevicesList);
}
if(StringUtils.isNotEmpty(fuelDevicesList)){
for(FuelDevice fuelDevice : fuelDevicesList){
String mqttDeviceName = fuelDevice.getMqttDeviceName();
String format = String.format(MqttConstants.MQTT_PREFIX, mqttDeviceName);
if(StringUtils.isNotEmpty(mqttDeviceName)){
String[] topics = {format + MqttConstants.MQTT_IS_ONLINE, //在线状态
format + MqttConstants.MQTT_SENSOR, //传感器数据
format + MqttConstants.MQTT_OILCAN, //实时油罐数据
format + MqttConstants.MQTT_TANK_TABLE, // 罐表数据
format + MqttConstants.MQTT_TANK_SET, //油罐数据
// format + MqttConstants.MQTT_POST_ALARM, //实时报警数据
format + MqttConstants.MQTT_ALARM, //报警记录
format + MqttConstants.MQTT_UNLOAD_OIL //卸油记录
};
for(int i = 0; i < topics.length; i++){
client.subscribe(topics[i], 1, mqttMessageListener);
}
}
}
}
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 断开连接
*/
public void disConnect(){
try {
client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 订阅主题
*/
public void subscribe(String topic){
try {
client.subscribe(topic, 1, mqttMessageListener);
} catch (MqttException e) {
e.printStackTrace();
connect();
}
}
public void subscribe(String topic[]){
try {
client.subscribe(topic);
} catch (MqttException e) {
e.printStackTrace();
connect();
}
}
/**
* 取消订阅主题
*
* @param topic 主题名称
*/
public void cleanTopic(String topic) {
if (client != null && client.isConnected()) {
try {
client.unsubscribe(topic);
} catch (MqttException e) {
e.printStackTrace();
}
} else {
System.out.println("取消订阅失败!");
}
}
/**
* 批量取消订阅主题
*
* @param topic 主题名称
*/
public void cleanTopic(String topic[]) {
if (client != null && client.isConnected()) {
try {
client.unsubscribe(topic);
} catch (MqttException e) {
e.printStackTrace();
}
} else {
System.out.println("取消订阅失败!");
}
}
/**
* 发布
*
* @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());
for(int i = 0; i < topic.length; i++){
MqttTopic mTopic = client.getTopic(topic[i]);
if (null == mTopic) {
logger.error("topic not exist");
}
MqttDeliveryToken token;
try {
token = mTopic.publish(message);
token.waitForCompletion();
} catch (MqttException e) {
e.printStackTrace();
connect();
}
}
}
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 = client.getTopic(topic);
if (null == mTopic) {
logger.error("topic not exist");
}
MqttDeliveryToken token;
try {
token = mTopic.publish(message);
token.waitForCompletion();
} catch (MqttPersistenceException e) {
e.printStackTrace();
connect();
} catch (MqttException e) {
e.printStackTrace();
connect();
}
}
}
package com.fuel.core.mqtt;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fuel.admin.service.ISysConfigService;
import com.fuel.common.constant.MqttConstants;
import com.fuel.common.core.redis.RedisCache;
import com.fuel.common.utils.DateUtils;
import com.fuel.common.utils.StringUtils;
import com.fuel.common.utils.spring.SpringUtils;
import com.fuel.equipment.service.IFuelDeviceService;
import com.fuel.equipment.service.ITankCapacityService;
import com.fuel.framework.manager.AsyncManager;
import com.fuel.mqttData.service.*;
import lombok.SneakyThrows;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@Component
public class MqttMessageListener implements IMqttMessageListener {
private static final Logger logger = LoggerFactory.getLogger(MqttMessageListener.class);
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
AsyncManager.me().execute(recordOper(topic, message));
}
public static TimerTask recordOper(String topic, MqttMessage message)
{
return new TimerTask()
{
@SneakyThrows
public void run()
{
}
};
}
}