1
votes

We are seeing an exception as mentioned in this topic title when trying to work on an Spring MQTT integration.

The setup details are as below: 1) We have two different message adapters: message-driven-channel-adapter and outbound-channel-adapter. Both have unique clientIDs

2) A desktop application is used as an Mqtt client to publish some messages which are handled by message-driven-channel-adapter. Sample code is as below:

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) A listner bean configured for message-driven-channel-adapter processes the incoming messages and also publishes a response to the outbound-channel-adapter's channel.

The sample code is as below:

    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));   
}

the above code publishes the message successfully but disconnects the 'inbound' message-driven-channel-adapter and we see a continuous stacktrace as below on the console:

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

Can someone please advise on the above?

Is there a way to make sure that the inbound adapter does not disconnect after a message is published to the outbound adapter?

regards.

1

1 Answers

0
votes

Need to see more StackTrace, but anyway I see the bad way to use Spring.

I think you should start from the Spring Core and study what is Dependency Injection and Inversion of Control.

If you say that you listen from <int-mqtt:inbound-channel-adapter>, I don't see reason why don't use <service-activator> and send messages to the <int-mqtt:outbound-channel-adapter>.

For this purpose, please, follow to the Spring Integration documentation to understand how to use POJO method invocation.

Anyway to create a new context for each message is bad idea at all...