2015-06-23 225 views
1

在尝试使用Spring MQTT集成时,我们看到本主题标题中提到的异常。Spring Mqtt:MqttPahoMessageDrivenChannelAdapter - 丢失的连接:连接丢失;正在重试

设置细节如下: 1)我们有两个不同的消息适配器:消息驱动通道适配器和出站通道适配器。两者都有唯一的客户ID

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)听者豆配置消息驱动通道适配器处理进入的消息,并还出版到出站信道适配器的信道响应。

的示例代码如下:

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... 

有人可以请建议上述吗?

有一种方法可以确保入站适配器在消息发布到出站适配器后不会断开连接吗?

关于。

回答

0

需要看到更多StackTrace,但无论如何,我看到使用Spring的坏方法。

我认为你应该从Spring Core开始,研究什么是依赖注入和控制反转。

如果你说你从<int-mqtt:inbound-channel-adapter>听,我看不到为什么不使用<service-activator>并发送消息到<int-mqtt:outbound-channel-adapter>

为此,请按照Spring Integration文档了解如何使用POJO方法调用。

反正创建每个消息一个新的上下文是坏主意......

+0

得到同样的错误.. https://stackoverflow.com/questions/44885702/spring-mqttpahomessagedrivenchanneladapter-lost-connectionconnection-lost -retr – HybrisFreelance