【问题标题】:Spring Mqtt: MqttPahoMessageDrivenChannelAdapter - Lost connection:Connection lost; retryingSpring Mqtt:MqttPahoMessageDrivenChannelAdapter - 丢失连接:连接丢失;重试
【发布时间】:2015-06-23 15:28:22
【问题描述】:

在尝试使用 Spring MQTT 集成时,我们看到了本主题标题中提到的异常。

设置详情如下: 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) 为消息驱动通道适配器配置的侦听器 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...

有人可以就上述问题提出建议吗?

有没有办法确保在消息发布到出站适配器后入站适配器不会断开连接?

问候。

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    需要查看更多 StackTrace,但无论如何我看到了使用 Spring 的糟糕方式。

    我觉得你应该从Spring Core开始,学习什么是依赖注入和控制反转。

    如果你说你从&lt;int-mqtt:inbound-channel-adapter&gt; 收听,我看不出为什么不使用&lt;service-activator&gt; 并向&lt;int-mqtt:outbound-channel-adapter&gt; 发送消息。

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

    无论如何为每条消息创建一个新的上下文是个坏主意......

    【讨论】:

    猜你喜欢
    • 2017-12-06
    • 2020-03-14
    • 1970-01-01
    • 2014-02-28
    • 2020-09-21
    • 1970-01-01
    • 2022-08-11
    • 2018-07-15
    • 2016-08-04
    相关资源
    最近更新 更多