|
@@ -22,6 +22,8 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
|
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
|
|
|
|
+import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
|
|
|
|
+import org.springframework.messaging.MessageChannel;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.context.ContextLoader;
|
|
import org.springframework.web.context.ContextLoader;
|
|
|
|
|
|
|
@@ -46,7 +48,11 @@ import java.util.stream.Collectors;
|
|
|
public class IotDataTransferService {
|
|
public class IotDataTransferService {
|
|
|
|
|
|
|
|
private MqttOutConfig.MqttGateway mqttGateway;
|
|
private MqttOutConfig.MqttGateway mqttGateway;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 注入ApplicationContext,确保总是能获取到
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ApplicationContext context;
|
|
|
|
|
+
|
|
|
// MQTT连接相关配置
|
|
// MQTT连接相关配置
|
|
|
private static final String MQTT_URL = "ssl://114.80.201.143:8883";
|
|
private static final String MQTT_URL = "ssl://114.80.201.143:8883";
|
|
|
private static final String MQTT_TOPIC = "iotInfo/+";
|
|
private static final String MQTT_TOPIC = "iotInfo/+";
|
|
@@ -898,25 +904,39 @@ public class IotDataTransferService {
|
|
|
*/
|
|
*/
|
|
|
private void createMqttConnection(String username, String password) {
|
|
private void createMqttConnection(String username, String password) {
|
|
|
try {
|
|
try {
|
|
|
- // 直接创建和配置MQTT客户端,不依赖Spring自动配置
|
|
|
|
|
- // 1. 创建MqttConnectOptions
|
|
|
|
|
|
|
+ // 使用注入的ApplicationContext获取已有的mqttGateway实例
|
|
|
|
|
+ // 因为我们保留了@MessagingGateway注解,Spring会自动创建这个实例
|
|
|
|
|
+ if (this.context == null) {
|
|
|
|
|
+ throw new IllegalStateException("ApplicationContext未注入,无法获取MQTT Gateway");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 获取mqttGateway实例
|
|
|
|
|
+ this.mqttGateway = this.context.getBean(MqttOutConfig.MqttGateway.class);
|
|
|
|
|
+ if (this.mqttGateway == null) {
|
|
|
|
|
+ throw new IllegalStateException("MQTT Gateway未找到,无法发送消息");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 获取现有的mqttClientFactory实例
|
|
|
|
|
+ DefaultMqttPahoClientFactory mqttClientFactory = this.context.getBean(DefaultMqttPahoClientFactory.class);
|
|
|
|
|
+ if (mqttClientFactory == null) {
|
|
|
|
|
+ throw new IllegalStateException("MQTT Client Factory未找到,无法创建MQTT连接");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 创建并配置MqttConnectOptions
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
options.setServerURIs(new String[]{MQTT_URL});
|
|
options.setServerURIs(new String[]{MQTT_URL});
|
|
|
options.setUserName(username);
|
|
options.setUserName(username);
|
|
|
options.setPassword(password.toCharArray());
|
|
options.setPassword(password.toCharArray());
|
|
|
options.setKeepAliveInterval(KEEP_ALIVE_INTERVAL);
|
|
options.setKeepAliveInterval(KEEP_ALIVE_INTERVAL);
|
|
|
-
|
|
|
|
|
- // 2. 创建DefaultMqttPahoClientFactory
|
|
|
|
|
- DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
|
|
|
|
- factory.setConnectionOptions(options);
|
|
|
|
|
-
|
|
|
|
|
- // 3. 由于@MessagingGateway需要Spring容器管理,我们需要确保Spring已经正确初始化了网关
|
|
|
|
|
- // 这里我们不手动创建网关,而是依赖Spring自动创建,但是需要确保MqttOutConfig类不再被Spring自动配置
|
|
|
|
|
- // 所以我们已经移除了MqttOutConfig和MqttInConfig类上的@Configuration注解和@Bean注解
|
|
|
|
|
- log.info("MQTT连接参数配置完成,用户名:{}", username);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 更新mqttClientFactory的连接选项
|
|
|
|
|
+ mqttClientFactory.setConnectionOptions(options);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("MQTT Gateway初始化成功,用户名:{}", username);
|
|
|
|
|
+ log.info("MQTT连接配置完成,服务器地址:{},客户端ID:mqttx-{}", MQTT_URL, username);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("创建MQTT连接失败: {}", e.getMessage(), e);
|
|
|
|
|
- throw new RuntimeException("创建MQTT连接失败", e);
|
|
|
|
|
|
|
+ log.error("初始化MQTT连接失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new RuntimeException("初始化MQTT连接失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|