【问题标题】:How to acknowledge message through program using Spring AMQP/Spring integration如何使用 Spring AMQP/Spring 集成通过程序确认消息
【发布时间】:2012-10-31 19:31:46
【问题描述】:

1) 服务器向客户端发送消息。

2) 入站通道适配器配置为等待来自消费者的“手动”确认模式操作

3)“TaskBundlereceiver”bean 正在实现“ChannelAwareMessageListener”,在实现方法中,我正在执行消息确认。

我没有看到“TaskBundlereceiver”被执行。我错过了什么吗?

下面是我已经解释的步骤的配置细节。

感谢您的投入。

    @Override
    public void onMessage(org.springframework.amqp.core.Message message, Channel channel) throws Exception 
    {
        logger.debug("In onMessage method of the channel aware listener. message =["+message.getBody().toString()+"]");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

XML 配置:

    <!-- Channel that receives the task bundle from the server for execution -->
    <int:channel id="fromKServerChannel"/>

    <int-amqp:inbound-channel-adapter id="taskBundleReceiverAdapter"
                                      channel="fromKServerChannel"
                                      error-channel="taskBundleErrorChannel"
                                      acknowledge-mode="MANUAL"
                                      expose-listener-channel="true"
                                      queue-names="kanga_task_queue"
                                      connection-factory="connectionFactory"
                                      concurrent-consumers="20"/>

    <int:chain input-channel="fromKServerChannel" output-channel="nullChannel">
        <int:service-activator ref="taskBundleReceiver" method="onMessage"/>
        <int:service-activator ref="taskBundleExecutor" method="executeBundle"/>
    </int:chain>

【问题讨论】:

    标签: spring-integration spring-amqp


    【解决方案1】:

    这样不行;侦听器是适配器,而不是通过服务激活器调用的服务。适配器目前不支持将通道传递给客户端进行手动确认。 Expose-listener-channel 属性是在使用事务时使用的,因此下栈兔子模板可以参与事务。

    为什么要手动确认? AUTO(默认)表示当线程正常返回时,容器会自动完成ack;如果你的服务抛出异常,消息将被 nack。

    所以,这就是控制 ack 的方法。

    如果您真的想使用 MANUAL 确认,则必须使用 &lt;rabbit:listener-container/&gt; 直接调用您的 taskBundleReceiver。然后它可以使用消息传递网关向执行者发送消息。

    【讨论】:

    • 谢谢加里。我正在一个单独的线程上对消息进行一些操作,并根据处理的成功/失败,我想相应地确认消息。入站通道适配器将传入消息路由到消息通道“fromKServerChannel”。您是否建议直接从“fromKServerChannel”调用的服务中调用“ChannelAwareMessageListener”实现类?我很困惑(显然)并感谢您的意见。
    • 否;入站通道适配器是一个从容器接收 onMessage() 调用的 MessageListener。它不支持 ChannelAwareMessagelistener。最简单的解决方案是,如果您希望消息被拒绝,则抛出异常,或者正常返回以获得 ack。如果你真的想调用 ChannelAwareMessageListener,你必须自己做;直接从 而不是 SI inbound-channel-adapter 接收消息。在大多数情况下,没有必要将 AMQP 暴露给用户代码。
    • 你的类可能只是一个 POJO,不知道它是从 Spring Integration 或 AMQP 调用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2022-01-07
    • 2019-10-06
    • 2019-06-21
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多