Spring AMQP-我的Spring应用程序可以为每个队列绑定多个交换吗



我有两个不同的交换机,我希望我的队列与之绑定,下面的方法不起作用

配置文件-

@Configuration
public class RabbitMQConfiguration {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Value("${spring.rabbitmq.batching.queue}")
private String batchingQueue;
@Value("${spring.rabbitmq.publishing.queue}")
private String publishingQueue;
@Value("${spring.rabbitmq.exchange}")
private String exchange;
@Value("${spring.rabbitmq.delayed.exchange}")
private String delayedExchange;
@Value("${spring.rabbitmq.batching.routingkey}")
private String batchingRoutingKey;
@Value("${spring.rabbitmq.publishing.routingkey}")
private String publishingRoutingKey;
@Bean
CachingConnectionFactory connectionFactory() {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(host);
cachingConnectionFactory.setUsername(username);
cachingConnectionFactory.setPassword(password);
return cachingConnectionFactory;
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
@Bean
Queue batchingQueue() {
return new Queue(batchingQueue, true);
}
@Bean
Queue publishingQueue() {
return new Queue(publishingQueue, true);
}
@Bean
Exchange myExchange() {
return ExchangeBuilder.directExchange(exchange).durable(true).build();
}
@Bean
public CustomExchange delayExchange() {
Map<String, Object> args = new HashMap<>();
args.put("x-delayed-type", "direct");
return new CustomExchange(delayedExchange, "x-delayed-message",true, false,args);
}
@Bean
Binding batchingBinding() {
return BindingBuilder
.bind(batchingQueue())
.to(myExchange())
.with(batchingRoutingKey)
.noargs();
}
@Bean
Binding publishingBinding() {
return BindingBuilder
.bind(publishingQueue())
.to(delayExchange())
.with(publishingRoutingKey)
.noargs();
}
}

当两个队列都与1个交换机(myExchange(绑定时,它可以完美地工作,但当我使用2个交换机运行此应用程序时,我会收到以下日志,最终它无法连接。请帮助我了解如何解决此

2021-07-08 16:40:27.358  INFO 6473 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-08 16:40:27.359  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:27.396  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:0/SimpleConnection@574e4184 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42744]
2021-07-08 16:40:27.423  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:27.425  WARN 6473 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2021-07-08 16:40:27.427  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:1/SimpleConnection@684bfba9 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42746]
2021-07-08 16:40:28.434  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:28.436  WARN 6473 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2021-07-08 16:40:28.444  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:2/SimpleConnection@63c66980 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42748]
2021-07-08 16:40:30.455  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:30.459  WARN 6473 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2021-07-08 16:40:30.466  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:3/SimpleConnection@5ce0f50a [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42754]
2021-07-08 16:40:34.476  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:34.477  WARN 6473 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2021-07-08 16:40:34.487  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:4/SimpleConnection@2111d7b9 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42756]
2021-07-08 16:40:39.493  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: localhost:5672
2021-07-08 16:40:39.494  WARN 6473 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2021-07-08 16:40:39.496  INFO 6473 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: connectionFactory#1d38cdde:5/SimpleConnection@65fe1f47 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 42764]
2021-07-08 16:40:39.496  INFO 6473 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.io.IOException

我意识到这是因为我还没有启用插件。

我需要先启用这个插件,然后它就工作了。

rabbitmq插件启用rabbitmq_delayerd_message_exchange

只要把它放出来,就会有其他人面对这个

最新更新