2015-06-23 284 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