在尝试使用Spring MQTT集成时,我们看到本主题 Headers 中提到的异常 .
设置详细信息如下:1)我们有两个不同的消息适配器:消息驱动通道适配器和出站通道适配器 . 两者都有唯一的clientID
2)桌面应用程序用作Mqtt客户端,以发布由消息驱动通道适配器处理的一些消息 . 示例代码如下:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
DefaultMqttPahoClientFactory mqttClientFactory = (DefaultMqttPahoClientFactory) ac.getBean("clientFactory");
MqttClient gatewayClient = mqttClientFactory.getClientInstance("tcp://abc.com:1883", "STCLP014CI021CLIENT1");
String test = "this is test"
gatewayClient.connect();
MqttMessage message = new MqttMessage(test.getBytes());
message.setQos(1);
gatewayClient.publish("store/entity/mpg/zmesg/fromdevice/014/00021/eco/pos/a56f4302004b1200/1420221417963/2ce6f45f-97a6-49d2-91e5-640effcfa651/192.168.10.70", message);
gatewayClient.disconnect();
3)为消息驱动通道适配器配置的列表器bean处理传入消息,并且还发布对出站通道适配器通道的响应 .
示例代码如下:
public void processMessage(String message)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("message received " + message);
String response = "response message";
MessageChannel responseChannel = (MessageChannel) ac.getBean("mpgResponseChannel");
responseChannel.send(new GenericMessage<String>(response));
}
上面的代码成功发布了消息,但断开了“入站”消息驱动通道适配器的连接,我们在控制台上看到如下的连续堆栈跟踪:
11:09:33.675 [MQTT Rec: STCLP014CI021CLIENT3] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retrying...
11:09:33.787 [MQTT Rec: STCLP014CI021CLIENT3] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retrying...
11:09:33.850 [MQTT Rec: STCLP014CI021CLIENT3] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retrying...
11:09:33.959 [MQTT Rec: STCLP014CI021CLIENT3] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retrying...
11:09:34.021 [MQTT Rec: STCLP014CI021CLIENT3] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retrying...
有人可以就上述建议吗?
在将消息发布到出站适配器后,有没有办法确保入站适配器不会断开连接?
问候 .
1 回答
需要看到更多StackTrace,但无论如何我看到使用Spring的坏方法 .
我认为你应该从Spring Core开始,研究什么是依赖注入和控制反转 .
如果你说你从
<int-mqtt:inbound-channel-adapter>
听,我不会't see reason why don'使用<service-activator>
并发送消息到<int-mqtt:outbound-channel-adapter>
.为此,请按照Spring Integration文档了解如何使用POJO方法调用 .
无论如何为每条消息创建一个新的上下文根本就是个坏主意......