在运行时构造它的最简单方法是什么?
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "providedAtRuntime", durable = "true"),
exchange = @Exchange(value = "providedAtRuntime", ignoreDeclarationExceptions = "true"),
key = "providedAtRuntime"), containerFactory = "cFac")
public class RabbitProcessor {
@RabbitHandler
public void receive (String smth){
System.out.println(smth);
}
}
我想定义监听器,但在运行时提供交换,队列名称和绑定。此侦听器也不应自动启动,而应由start()方法调用。同时它应该自动声明绑定和队列等。当调用stop()时,它应该停止消耗。
答案 0 :(得分:5)
我认为注释无法实现,但您可以创建自定义SimpleMessageListenerContainer
。
这是一个简单的解决方案:
public static AbstractMessageListenerContainer startListening(RabbitAdmin rabbitAdmin, Queue queue, Exchange exchange, String key, MessageListener messageListener) {
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(key).noargs());
SimpleMessageListenerContainer listener = new SimpleMessageListenerContainer(rabbitAdmin.getRabbitTemplate().getConnectionFactory());
listener.addQueues(queue);
listener.setMessageListener(messageListener);
listener.start();
return listener;
}
你可以称之为:
ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
ConnectionFactory connectionFactory = ctx.getBean(ConnectionFactory.class);
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
AbstractMessageListenerContainer container = startListening(rabbitAdmin, rabbitAdmin.declareQueue(),
new DirectExchange("amq.direct"), "testRoute", message -> {
System.out.println(new String(message.getBody()));
});
你可以
AbstractMessageListenerContainer.destroy()
或AbstractMessageListenerContainer.stop()
。
使用spring boot 1.5.8.RELEASE和RabbitMQ 3.6.10进行测试