根据标头属性(例如路由键)处理入站 AMQP 消息
我有一项服务,可以接收AMQP消息。该服务与队列绑定,该队列接收所有与一组路由密钥匹配的消息。
我的设置如下:
...
private SomeController controller;
@Autowired
private SimpleMessageListenerContainer receiverContainer;
@Bean
public IntegrationFlow inboundFlow(){
var adpater = Amqp.inboundAdapter(receiverContainer);
return IntegrationFlows.from(adapter)
// some transformations
.handle(controller, "processMessage")
.get();
}
这已经很好。但是,现在我想根据标头属性处理不同控制器的消息。在这种情况下,我想为每个路由键提供一个控制器。使用一个带有多个路由键的单个队列只是为每个密钥以不同的方式处理它也是一个好主意吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在交换和单个队列之间拥有多个绑定确实是合法的。
在本教程中查看更多信息: https://wwwww.rabbitmq.com /tutorials/tutorial-four-spring-amqp.html 。
amqp.inboundadapter()
依赖于defaultAmqpheaderMapper.inboundmapper()
默认情况下,该 by默认情况下为我们填充了amqpheaders.received_routing_routing_key
meesse header。因此,您确实可以使用route(message.class,m-> m.getheaders()。路由键值。
It is really legit to have several bindings between an exchange and a single queue.
See more info in this tutorial: https://www.rabbitmq.com/tutorials/tutorial-four-spring-amqp.html.
The
Amqp.inboundAdapter()
relies on theDefaultAmqpHeaderMapper.inboundMapper()
by default which populates for us anAmqpHeaders.RECEIVED_ROUTING_KEY
message header before producing. So, you indeed can use aroute(Message.class, m -> m.getHeaders().get(AmqpHeaders.RECEIVED_ROUTING_KEY))
with appropriatechannelMapping()
for the routing key value.我只是想添加一个代码示例,并入 artem bilan 's(正确)答案,因为此外,我必须合并一个网关(由Artem Bilan暗示了“适当的
channelmapping()
)。更多有关您为什么需要网关或某些情况下的桥梁,请参阅
我的初始代码snippit变成了以下内容:
I just wanted to add a code example, incorporating Artem Bilan's (correct) answer, because additionally to that, I had to incorporate a gateway (hinted by Artem Bilan with "appropriate
channelMapping()
).More about why you need a gateway or in some cases a bridge, refer to this part of the documentation.
My initial code snippit becomes something like the following: