java - Spring Integration 和 RabbitMQ,如何避免对入站 channel 适配器进行轮询

标签 java rabbitmq spring-integration amqp

我目前有一个带有直接交换的 Rabbit MQ Broker 设置,并且我正在使用 Spring Integration 来生成和使用队列中的消息。我使用责任链模式,使用 SI 将使用的消息从一个 POJO 传递到另一个 POJO。现在,为了执行此操作,我使用了 Spring Integration 入站 channel 适配器。这要求它有一个对轮询器的引用。

如何删除轮询器,以便它不在轮询机制上工作,而是使用回调工作,以便一旦消息可用,它就会从队列中获取它,同时仍然能够使用由SI。

我尝试将入站 channel 适配器更改为 Spring Rabbit 入站 channel 适配器,但是无法将消息转发到我的 POJO。不支持以下内容。

ref="eventConsumer" method="onReceiveEvent"

我在下面定义了一些简单的配置,如何在不需要轮询的情况下实现相同的效果?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="classpath:datasource-config.xml" />

    <int:logging-channel-adapter id="logger" level="DEBUG" log-full-message="true" />

    <int:channel id="eventChannel">
        <int:interceptors>
            <int:wire-tap channel="logger" />
        </int:interceptors>
    </int:channel>

    <int:channel id="errorChannel">
        <int:interceptors>
            <int:wire-tap channel="logger" />
        </int:interceptors>
    </int:channel>

    <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
        <property name="host" value="${rabbitmq.host}" />
        <property name="port" value="${rabbitmq.port}" />
        <property name="username" value="${rabbitmq.username}" />
        <property name="password" value="${rabbitmq.password}" />
        <property name="virtualHost" value="${rabbitmq.events.virtual.host}" />
    </bean>

    <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
        <constructor-arg name="connectionFactory" ref="connectionFactory" />
        <property name="exchange" value="${rabbitmq.events.exchange.name}" />
        <property name="routingKey" value="${rabbitmq.events.routing.key}" />
    </bean>

    <bean id="pollingInterval" class="java.lang.String">
        <constructor-arg value="${rabbitmq.message.polling.interval}" />
    </bean>

    <int:poller id="rabbitConsumerPoller" fixed-rate="#{pollingInterval}" error-channel="errorChannel" />

    <bean id="eventConsumer" class="com.idna.events.consumer.EventsConsumer">
        <property name="rabbitTemplate" ref="rabbitTemplate"/>
        <property name="consumerDao" ref="eventsConsumerDao" />
        <property name="queueName" value="${rabbitmq.events.queue.name}" />
    </bean>

    <int:inbound-channel-adapter id="inboundChannelAdapter" channel="eventChannel" ref="eventConsumer" method="onReceiveEvent">
        <int:poller ref="rabbitConsumerPoller"/>
    </int:inbound-channel-adapter>

</beans>

谢谢!

最佳答案

使用 将消息发送到您的 eventChannel。您的消费者不需要了解兔子;它可以通过

调用

关于java - Spring Integration 和 RabbitMQ,如何避免对入站 channel 适配器进行轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123579/

相关文章:

java - RabbitMQ 连接处于阻塞状态?

rabbitmq - 使用内存总线对公共(public)交通中的发布方法进行单元测试

java - ConcurrentHashMap 和锁

java - Java代码如何向Spark注册无参数UDF

java - 尝试从数组中随机选择一个值时出现错误

java - 如何通过 Rest API 使用提交 Id 从 Bitbucket 下载文件?

python - 在pytest中模拟一个连接类

spring-integration - Spring Integration Aggregator 组超时值的问题

spring - 将轮询器添加到 SFTP 出站网关

spring-integration - spring 集成 : inbound channel adapter. 捕获异常