MQTT 应使用哪个 Spring 集成频道

Which Spring Integration Channel should be used for MQTT

我正在尝试一个演示应用程序,我们通过 UI 发送命令,然后通过 spring 集成将命令传送到设备并将命令状态返回给 UI, 这是我第一次使用 Spring Integration,我对渠道的各种实现有点不确定。 MQTT 不提供 P2P 直接通信,因此我应该为流中的入站和出站适配器使用什么通道,PublishSubscribe 还是 Direct?

我认为你需要知道的不只是 "which channel type should I use?"。

如果你的意思是你想向设备发送命令并等待回复并将回复发送给浏览器,你需要将本质上异步的协议转换为同步 request/reply 场景。

您可能需要挂起 http 请求线程,直到收到来自设备的异步回复,将回复与请求相关联,并在将回复移交给它后释放请求线程。

您可以简单地在 @Controller 中进行关联,然后将回复发送到控制器中的其他方法,在那里进行关联,然后释放 http 线程。

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

has one technique for a similar use case; we are considering adding a component to the framework 使此类场景更易于实施。

无论如何,您将使用 DirectChannels 来连接组件。

如果我完全误解了你的问题,请澄清。