简体   繁体   English

春季从MQTT出站通道适配器获取事件

[英]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.

相关问题 int-jms 的 Java 配置:来自 Spring 集成的出站通道适配器 - Java configuration for int-jms:outbound-channel-adapter from spring integration 为什么在来自int-jms:outbound-channel-adapter的MQException情况下不会调用errorChannel? - Why errorChannel is not getting called in case of MQException from int-jms:outbound-channel-adapter? Spring-Integration-Kafka出站通道适配器发送消息 - Spring-Integration-Kafka outbound-channel-adapter Send message Spring Integration Java DSL中的JPA出站通道适配器配置 - JPA outbound channel adapter config in Spring Integration Java DSL Spring Integration outbound-channel-adapter destination-expression MQMDMessageContext - Spring Integration outbound-channel-adapter destination-expression MQMDMessageContext 如何使用Spring集成或出站通道适配器将单个文件发送到多个主机 - How to send a single file to multiple host using spring integration or using outbound channel adapter 如何使Spring Integration HTTP outbound-channel-adapter参与全局事务 - How to make Spring Integration HTTP outbound-channel-adapter participate in Global Transaction Spring 集成 - 线程出站通道 - Spring Integration - Threaded Outbound Channel 为什么SFTP入站/出站通道适配器有单独的通道声明,为什么没有简单文件入站/出站通道适配器的通道声明? - Why there is separate channel declaration for SFTP inbound/outbound channel adapter and why not for simple file inbound/outbound channel adapter? 多个出站标准输出通道适配器不起作用 - Multiple outbound stdout channel adapter not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM