Java 带RoutingConnectionFactory的Spring RabbitListener
我设置了一个Java 带RoutingConnectionFactory的Spring RabbitListener,java,spring,spring-amqp,Java,Spring,Spring Amqp,我设置了一个SimpleRoutingConnectionFactory如下: @Bean @Primary public SimpleRoutingConnectionFactory routingConnectionFactory(ConnectionFactory cf1, ConnectionFactory cf2) { SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory(); M
SimpleRoutingConnectionFactory
如下:
@Bean
@Primary
public SimpleRoutingConnectionFactory routingConnectionFactory(ConnectionFactory cf1, ConnectionFactory cf2) {
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> map = new HashMap<>();
map.put("[foo.server1,bar.server1]", cf1); // receive
map.put("[foo.server2,bar.server2]", cf2); // receive
map.put("server1", cf1); // send
map.put("server2", cf2); // send
rcf.setTargetConnectionFactories(map);
rcs.setDefaultConnectionFactory(defaultConnectionFactory());
return rcf;
}
@Bean
@初级的
公共SimpleRoutingConnectionFactory路由ConnectionFactory(ConnectionFactory cf1,ConnectionFactory cf2){
SimpleRoutingConnectionFactory rcf=新的SimpleRoutingConnectionFactory();
Map Map=newhashmap();
map.put(“[foo.server1,bar.server1]”,cf1);//接收
map.put([foo.server2,bar.server2],cf2);//接收
map.put(“server1”,cf1);//发送
map.put(“server2”,cf2);//发送
rcf.SetTargetConnectionFactorys(地图);
setDefaultConnectionFactory(defaultConnectionFactory());
返回循环贷款;
}
上面的RoutingConnectionFactory
允许我按预期使用SimpleResourceHolder.bind(rabbitTemplate().getConnectionFactory(),host)
在特定服务器上发送消息,但接收时没有
然后,我手动注册了一个侦听foo.server1、bar.server1、foo.server2、bar.server2
队列的MessageListenerContainer
我希望看到由cf1
为发送到foo.server1
的消息创建的频道,以及由cf2
为发送到bar.server2
的消息创建的频道,但频道似乎是由defaultConnectionFactory
创建的
但是,我可以在调试日志中看到:
无法确定查找键[[foo.server1,bar.server1,foo.server2,bar.server2]]的目标ConnectionFactory
这意味着MessageListenerContainer
对所有队列使用单个ConnectionFactory
?我是否可以将侦听器配置为每个队列使用不同的工厂?我尝试为每个队列注册一个工厂,而不是将它们分组,但这也不起作用
这意味着MessageListenerContainer对所有队列使用单个ConnectionFactory
这是正确的
我是否可以将侦听器配置为每个队列使用不同的工厂?我尝试为每个队列注册一个工厂,而不是将它们分组,但这也不起作用
没有;每个队列都需要一个单独的容器。您还可以将真正的连接工厂绑定到它,而不是使用路由工厂。我将创建多个容器,并使用MessageChannel来统一消息。。。谢谢