【问题标题】:Add default method before calling onMessage if MessageListener of Spring Amqp如果 Spring Amqp 的 MessageListener,在调用 onMessage 之前添加默认方法
【发布时间】:2016-04-04 10:46:52
【问题描述】:

在调用实现类的 onMessage 功能之前,我需要添加一些默认功能,这些功能将从 Spring Amqp 中的 Message 标头中选取一些值。

例如

class TestListener implements MessageListerner {

public void onMessage(Message message){
// some logic 
   }
}

从上面的示例中,我需要在调用 onMessage 之前使用 Message 对象调用一些自定义功能。

有什么优雅的方法可以在 SpringAmqp 中实现同样的效果

【问题讨论】:

  • 我不明白这个问题,或者背后的原因......如果您想在收到之前更改消息,请在发送之前执行此操作。或者,也许您只是想使用标头交换之类的东西?
  • 要求是在Message得到onMessage之前,在listener端进行任何业务操作。例如。从生产者端设置消息头中的值说线程名称 = t1,在侦听器端,我将检索该值并对该 t1 执行任何通用操作以获取其他消息,线程名称的值可以是 t2 等。
  • 但是onMessage 仅在收到消息时才被调用(或者我遗漏了什么)?

标签: rabbitmq spring-amqp spring-rabbit


【解决方案1】:

你可以在你的监听器周围创建一个简单的包装类。

public void onMessage(Message message) {
    // process
    myListener.onMessage(message)
}

或者,侦听器容器有一个属性afterReceivePostProcessors 正是因为这个原因......

/**
 * Set {@link MessagePostProcessor}s that will be applied after message reception, before
 * invoking the {@link MessageListener}. Often used to decompress data.  Processors are invoked in order,
 * depending on {@code PriorityOrder}, {@code Order} and finally unordered.
 * @param afterReceivePostProcessors the post processor.
 * @since 1.4.2
 */
public void setAfterReceivePostProcessors(MessagePostProcessor... afterReceivePostProcessors) { ... }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2015-01-24
    • 2012-12-12
    • 1970-01-01
    • 2021-05-14
    • 2014-09-25
    相关资源
    最近更新 更多