Spring AMQP:使用程序化端点注册时,如何设置路由键?

Spring AMQP: When using programmatic endpoint registration, how to set routing key?

我正在使用侦听器端点的编程端点注册:

MethodRabbitListenerEndpoint endpoint = new MethodRabbitListenerEndpoint();
endpoint.setId(endpointId);
endpoint.setQueues(eventsQueue);
endpoint.setBean(hanlderMethod.bean);
endpoint.setMethod(hanlderMethod.method);
endpoint.setMessageHandlerMethodFactory(messageHandlerMethodFactory);
registrar.registerEndpoint(endpoint);

我的问题是,如何确定此端点的路由密钥?


编辑: 为了进一步说明,我对不同类型的消息使用单个队列,我想根据路由键将它们路由到不同的方法。这是对用于将消息路由到此队列的路由键的补充。

基本上,用例是通用事件总线。所有事件都进行相同的交换。每种类型的事件都有一个唯一的路由键。每个服务都有一个事件队列。每个服务通过使用该事件类型的路由键在事件交换和它自己的事件队列之间添加适当的绑定来订阅它感兴趣的事件。每种事件类型都有不同的处理方法。

看,你说 Listener,所以你要听一些 queue 的消息。

没错,你可以通过 setQueues() 来做到这一点。

现在关于 routingKey:

The routing key is a message attribute. The exchange might look at this key when deciding how to route the message to queues (depending on exchange type).

所以,它确实与 Listener 无关。

虽然我同意我们在处理queue时应该在那个地方声明Binding。因此在听众部分。

因此,如果您手动 MethodRabbitListenerEndpoint 注册(绕过 @RabbitListener 定义),您也应该手动声明和注册 Binding。并且已经在这里有一个适当的routingKeyhttp://docs.spring.io/spring-amqp/reference/html/_reference.html#_binding

更新

没有您要查找的 built-in 功能。 我们有 MultiMethodRabbitListenerEndpoint 谁根据有效负载类型进行路由,但没有任何其他可能的过滤器。

你想要的可以通过 Spring 集成路由器实现,它可以根据 AmqpHeaders.RECEIVED_ROUTING_KEY header.

做出决定

从另一方面来说,也许最好为每个 routing key 注册唯一的 queue,并使用适当的方法为该 queue 只有一个可能的侦听器。