我有一个应用程序,它将侦听多个队列,这些队列在不同的vhost上声明。我使用SimpleRoutingConnectionFactory来存储connectionFactoryMap,我希望用@RabbitListener设置我的监听器。
根据Spring AMQP doc:
同样从1.4版开始,您可以配置路由连接 SimpleMessageListenerContainer中的工厂。在那种情况下,列表 队列名称用作查找键。例如,如果您配置 使用setQueueNames(" foo,bar")的容器,查找键将是 " [富,巴]" (没有空格)。
我使用了@RabbitListener(queues = "some-key")
。不幸的是,春天抱怨"查找键[null]"。见下文。
18:52:44.528警告--- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer:消费者引发异常, 如果连接工厂支持,处理可以重新启动 java.lang.IllegalStateException:无法确定目标 ConnectionFactory for lookup key [null] at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.determineTargetConnectionFactory(AbstractRoutingConnectionFactory.java:119) 在 org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.createConnection(AbstractRoutingConnectionFactory.java:97) 在 org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils $ 1.createConnection(ConnectionFactoryUtils.java:90) 在 org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:140) 在 org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:76) 在 org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:472) 在 org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1306) 在java.lang.Thread.run(Thread.java:745)
我做错了什么吗?如果将queues属性用作查找键(用于连接工厂查找),我应该用什么来指定我想听哪个队列?
最终,我希望能够进行编程/动态侦听器设置。如果我使用"程序化端点注册",我是否应该删除#34;注释驱动的侦听器端点"?我喜欢"注释驱动的侦听器端点",因为侦听器可以有多个带有不同传入数据类型的消息句柄作为参数,这非常干净整洁。如果我使用Programmatic Endpoint Registration,我将不得不解析Message输入变量,并根据消息类型/内容调用我的特定自定义消息处理程序。
修改 嗨加里, 我稍微修改了你的代码#2,以便它使用Jackson2JsonMessageConverter来序列化类对象(在RabbitTemplate bean中),并使用它将它们反序列化回对象(在inboundAdapter中)。我还删除了@RabbitListener,因为在我的情况下,所有的监听器都会在运行时添加。现在fooBean可以接收整数,字符串和TestData消息而没有任何问题!留下的唯一问题是该计划不断报告警告:
" [erContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer:消费者引发异常,如果连接工厂支持,处理可以重启
java.lang.IllegalStateException:无法为查找键[null]"确定目标ConnectionFactory。有关完整的堆栈跟踪,请参阅底部。
我错过了什么吗?
@SpringBootApplication
public class App2 implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Autowired
private IntegrationFlowContext flowContext;
@Autowired
private ConnectionFactory routingCf;
@Autowired
private RabbitTemplate template;
@Override
public void run(String... args) throws Exception {
// dynamically add a listener for queue qux
IntegrationFlow flow = IntegrationFlows.from(Amqp.inboundAdapter(this.routingCf, "qux").messageConverter(new Jackson2JsonMessageConverter()))
.handle(fooBean())
.get();
this.flowContext.registration(flow).register();
// now test it
SimpleResourceHolder.bind(this.routingCf, "[qux]");
this.template.convertAndSend("qux", 42);
this.template.convertAndSend("qux", "fizbuz");
this.template.convertAndSend("qux", new TestData(1, "test"));
SimpleResourceHolder.unbind(this.routingCf);
}
@Bean
RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(routingCf);
template.setMessageConverter(new Jackson2JsonMessageConverter());
return template;
}
@Bean
@Primary
public ConnectionFactory routingCf() {
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> map = new HashMap<>();
map.put("[foo,bar]", routedCf());
map.put("[baz]", routedCf());
map.put("[qux]", routedCf());
rcf.setTargetConnectionFactories(map);
return rcf;
}
@Bean
public ConnectionFactory routedCf() {
return new CachingConnectionFactory("127.0.0.1");
}
@Bean
public Foo fooBean() {
return new Foo();
}
public static class Foo {
@ServiceActivator
public void handleInteger(Integer in) {
System.out.println("int: " + in);
}
@ServiceActivator
public void handleString(String in) {
System.out.println("str: " + in);
}
@ServiceActivator
public void handleData(TestData data) {
System.out.println("TestData: " + data);
}
}
}
完整堆栈跟踪:
2017-03-15 21:43:06.413 INFO 1003 --- [ main] hello.App2 : Started App2 in 3.003 seconds (JVM running for 3.69)
2017-03-15 21:43:11.415 WARN 1003 --- [erContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it
java.lang.IllegalStateException: Cannot determine target ConnectionFactory for lookup key [null]
at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.determineTargetConnectionFactory(AbstractRoutingConnectionFactory.java:119) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.createConnection(AbstractRoutingConnectionFactory.java:97) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1430) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1387) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.core.RabbitAdmin.initialize(RabbitAdmin.java:500) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.core.RabbitAdmin$11.onCreate(RabbitAdmin.java:419) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.CompositeConnectionListener.onCreate(CompositeConnectionListener.java:33) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:571) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:90) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:140) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:76) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:505) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1382) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_112]
答案 0 :(得分:0)
请显示您的配置 - 它对我来说很好......
@SpringBootApplication
public class So42784471Application {
public static void main(String[] args) {
SpringApplication.run(So42784471Application.class, args);
}
@Bean
@Primary
public ConnectionFactory routing() {
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> map = new HashMap<>();
map.put("[foo,bar]", routedCf());
map.put("[baz]", routedCf());
rcf.setTargetConnectionFactories(map);
return rcf;
}
@Bean
public ConnectionFactory routedCf() {
return new CachingConnectionFactory("10.0.0.3");
}
@RabbitListener(queues = { "foo" , "bar" })
public void foobar(String in) {
System.out.println(in);
}
@RabbitListener(queues = "baz")
public void bazzer(String in) {
System.out.println(in);
}
}
关于第二个问题,您可以手动构建端点,但它非常复杂。在Spring Integration @ServiceActivator
中使用类似功能可能更容易。
我会尽快更新此答案。
修改强>
以下是使用Spring Integration技术在运行时动态添加多方法侦听器的更新...
@SpringBootApplication
public class So42784471Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(So42784471Application.class, args);
}
@Autowired
private IntegrationFlowContext flowContext;
@Autowired
private ConnectionFactory routingCf;
@Autowired
private RabbitTemplate template;
@Override
public void run(String... args) throws Exception {
// dynamically add a listener for queue qux
IntegrationFlow flow = IntegrationFlows.from(Amqp.inboundAdapter(this.routingCf, "qux"))
.handle(fooBean())
.get();
this.flowContext.registration(flow).register();
// now test it
SimpleResourceHolder.bind(this.routingCf, "[qux]");
this.template.convertAndSend("qux", 42);
this.template.convertAndSend("qux", "fizbuz");
SimpleResourceHolder.unbind(this.routingCf);
}
@Bean
@Primary
public ConnectionFactory routingCf() {
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> map = new HashMap<>();
map.put("[foo,bar]", routedCf());
map.put("[baz]", routedCf());
map.put("[qux]", routedCf());
rcf.setTargetConnectionFactories(map);
return rcf;
}
@Bean
public ConnectionFactory routedCf() {
return new CachingConnectionFactory("10.0.0.3");
}
@RabbitListener(queues = { "foo" , "bar" })
public void foobar(String in) {
System.out.println(in);
}
@RabbitListener(queues = "baz")
public void bazzer(String in) {
System.out.println(in);
}
@Bean
public Foo fooBean() {
return new Foo();
}
public static class Foo {
@ServiceActivator
public void handleInteger(Integer in) {
System.out.println("int: " + in);
}
@ServiceActivator
public void handleString(String in) {
System.out.println("str: " + in);
}
}
}