[英]Spring getting events from the MQTT outbound channel adapter
I am already using the Spring Integration 4.1.0 SNAPSHOT. 我已经在使用Spring Integration 4.1.0 SNAPSHOT。
I have this MQTT outbound adapter: 我有此MQTT出站适配器:
<int-mqtt:outbound-channel-adapter
async="true"
async-events="true"
id="mqttOutput"
channel="httpInputChannel"
client-id="#{controller.mqttPublisherConfig.clientID}"
url="#{controller.mqttPublisherConfig.completeURL}"
default-qos="#{controller.mqttPublisherConfig.qosString}"
default-retained="#{controller.mqttPublisherConfig.retainFlag}"
default-topic="#{controller.mqttPublisherConfig.topic}"
/>
Now in my Controller (MVC app), I want to receive the events emitted by the adapter. 现在,在我的控制器(MVC应用程序)中,我想接收适配器发出的事件。
I am implementing ApplicatinListener: 我正在实现ApplicatinListener:
@Controller
public class ServletController implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event)
{
//
}
...
}
Still , I am not receiving any events from the MQTT adapter. 仍然 ,我没有从MQTT适配器收到任何事件。
Implementing an in-event:inbound-channel-adapter works, though: 不过,实现事件中:入站通道适配器的工作原理是:
<int-event:inbound-channel-adapter channel="eventLogger"
error-channel="eventErrorChannel"
/>
But I would really like to handle the events in the code! 但是我真的很想处理代码中的事件!
Is your @Controller
in the same application context as the MQTT adapter? 您的
@Controller
是否与MQTT适配器在同一应用程序上下文中? Or (as is most common), the controller is in the web ( DispatcherServlet
's) context and the other beans are in the root application context loaded by the ContextLoaderListener
. 或者(最常见),控制器在Web(
DispatcherServlet
的)上下文中,而其他bean在ContextLoaderListener
加载的根应用程序上下文中。
The problem is beans in the root context cannot "see" beans in the servlet context and events published in the root context are not received by listeners in the child context. 问题在于,根上下文中的bean无法在servlet上下文中“看到” bean,并且子上下文中的侦听器无法接收在根上下文中发布的事件。
You would have to subvert this visibility issue - either move the business beans into the web context (not generally recommended, but not terrible for a small application) or somehow wire a listener from the root context into the controller - perhaps by wiring it into the controller and have the controller pass itself into the listener during initialization ( afterPropertiesSet()
). 您将不得不颠覆这种可见性问题-将业务Bean移到Web上下文中(通常不建议使用,但对于小型应用程序并不可怕),或者以某种方式将侦听器从根上下文连接到控制器中-也许通过将其连接到控制器中。控制器,并在初始化期间将控制器本身传递给侦听器(
afterPropertiesSet()
)。 You'd have a class tangle (mutual dependency), but it should work. 您会有一个类纠缠(相互依赖),但它应该可以工作。
By the way, ApplicationListener
can take generics so 顺便说一句,
ApplicationListener
可以采用泛型,因此
public class MyListener implements ApplicationListener<MqttIntegrationEvent> { ... }
will only get MQTT events. 将仅获取MQTT事件。
EDIT: 编辑:
Another solution would be to use the event channel adapter and add an outbound-channel-adapter
to the web context (the same context as the controller)... 另一个解决方案是使用事件通道适配器,并将
outbound-channel-adapter
通道适配器添加到Web上下文(与控制器相同的上下文)...
@Controller
...
public void onMqttEvent(MqttIntegrationEvent event) { ... }
<int:outbound-channel-adapter channel="eventLogger"
ref="myController" method="onMqttEvent" />
It will have visibility to the channel in the root context. 它将在根上下文中对通道具有可见性。
Be sure to configure the event adapter to only receive MqttIntegrationEvent
s. 确保将事件适配器配置为仅接收
MqttIntegrationEvent
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.