Spring-AMQP-基于消息头的路由

问题描述 投票:0回答:1

根据文档:https://docs.spring.io/spring-amqp/docs/2.2.5.RELEASE/reference/html/#async-annotation-driven

我们可以根据转换后的类类型为消息设置不同的处理程序,例如:

@RabbitListener(id="multi", queues = "someQueue")
@SendTo("my.reply.queue")
public class MultiListenerBean {

    @RabbitHandler
    public String thing2(Thing2 thing2) {
        ...
    }

    @RabbitHandler
    public String cat(Cat cat) {
        ...
    }

    @RabbitHandler
    public String hat(@Header("amqp_receivedRoutingKey") String rk, @Payload Hat hat) {
        ...
    }

    @RabbitHandler(isDefault = true)
    public String defaultMethod(Object object) {
        ...
    }

}

我相信这在性能上不会很出色,因为它必须进行反复试验才能将传入的有效负载转换为有效值。

相反,如何根据条件过滤标头值?如果header ['operation'] =“ order”,则将消息有效载荷强制转换为Order类。

spring-boot spring-amqp spring-rabbitmq spring-rabbit
1个回答
0
投票

我相信这在性能上不会很出色,因为它必须进行反复试验才能将传入的有效载荷强制转换。

通常,类型信息在标头中传送,MessageConverter使用该信息创建有效负载-不存在“反复试验”。

如果不使用提供的转换器之一,则可以基于header['operation']创建自己的转换器。

© www.soinside.com 2019 - 2024. All rights reserved.