47 lines
1.3 KiB
Java
47 lines
1.3 KiB
Java
package com.fivegroup.service;
|
||
|
||
import com.fivegroup.config.MqttChannel;
|
||
import com.fivegroup.utils.MqttCallBacks;
|
||
import org.eclipse.paho.client.mqttv3.*;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.integration.annotation.ServiceActivator;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import javax.annotation.PostConstruct;
|
||
|
||
/**
|
||
* 收到mqtt的消息分流给kafka
|
||
*
|
||
* @author ZhangXushuo
|
||
* @version 2023/12/1 - 19:00
|
||
*/
|
||
@Service
|
||
public class ShuntService {
|
||
@Autowired
|
||
MqttCallBacks mqttCallBack;
|
||
@Autowired
|
||
private MqttChannel mqttChannel;
|
||
|
||
/**
|
||
* 在这个示例中,我们使用@ServiceActivator注解将handleMessage方法标记为消息处理器。
|
||
* 该方法会在接收到MQTT消息时被调用。通过mqtt_receivedTopic头部可以获取到消息的主题,
|
||
* 通过message.getPayload()可以获取到消息的内容。
|
||
*/
|
||
@PostConstruct
|
||
@ServiceActivator(inputChannel = "mqttInputChannel")
|
||
public void mqttAc() {
|
||
// 在这里处理接收到的MQTT消息
|
||
try {
|
||
IMqttClient client = mqttChannel.mqttClient();
|
||
client.connect();
|
||
client.subscribe("five");
|
||
client.setCallback(mqttCallBack);
|
||
} catch (MqttException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|