Which Spring Integration Channel should be used for MQTT

503 views Asked by At

I am trying a demo app where we send commands over a UI which then goes through spring integration to deliver a command to a device and gives a command status back to UI, this is my first time using Spring Integration and am a bit unsure of the various implementations of channels. MQTT does not provide P2P direct communication, hence what channel should I use for Inbound and Outbound Adapters in my flow, PublishSubscribe or Direct?

1

There are 1 answers

5
Gary Russell On BEST ANSWER

I think you need to know more than "which channel type should I use?".

If you mean you want to send a command to the device and wait for a reply and send that reply to the browser, you need to turn an essentially asynchronous protocol into a synchronous request/reply scenario.

You would somehow need to to suspend the http request thread until the async reply from the device is received, correlate the reply to the request and release the request thread after handing over the reply to it.

You could simply do the correlation in your @Controller and send the replies to some other method in the controller, do the correlation there, and release the http thread.

<int-mqtt:inbound-channel-adapter channel="results" .../>
<int:channel id="results" />
<int:service-activator ref="myController" 
    method="someMethodToReceiveTheReplyAndCorrelateToRequest" />

This answer has one technique for a similar use case; we are considering adding a component to the framework to make such scenarios simpler to implement.

In any case, you would use DirectChannels to connect the components.

If I have completely misunderstood your question, please clarify.