baoshiwei
2025-06-19 be00ddc83f86599916eb8d0f581f448aa74c9d51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.zhitan.config.mqtt;
 
import com.zhitan.handler.MqttMessageHandler;
import com.zhitan.service.IDataService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
 
@Configuration
public class MqttInboundConfig {
 
    private final MqttPahoClientFactory mqttClientFactory;
 
    private final IDataService dataService;
 
    @Value("${spring.mqtt.client-id}")
    private String clientId;
 
    @Value("${spring.mqtt.default-topic}")
    private String defaultTopic;
 
    public MqttInboundConfig(MqttPahoClientFactory mqttClientFactory, IDataService dataService) {
        this.mqttClientFactory = mqttClientFactory;
        this.dataService = dataService;
    }
 
    // 订阅消息适配器
    @Bean
    public MqttPahoMessageDrivenChannelAdapter inboundAdapter() {
        return new MqttPahoMessageDrivenChannelAdapter(clientId + "-inbound", mqttClientFactory, defaultTopic,MqttTopic.DEVICE_KT_UP);
    }
 
    // 定义消息处理流
    @Bean
    public IntegrationFlow mqttInFlow() {
        return IntegrationFlows.from(inboundAdapter())
                .handle(new MqttMessageHandler(dataService))
                .get();
    }
}