首页 > 解决方案 > Spring AMQP - Can my spring app have multiple exchanges binding to each queues?

问题描述

I have 2 different exchanges I want my queues to bind with, Below approach is not working

Configuration File -

@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();
    }
}

When both the queues are binding with 1 exchange(myExchange) it works perfectly, But when I run this application with 2 exchanges I get following logs and ultimately it doesn't get connected. Please help me in understanding how I can resolve this

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

标签: javarabbitmqspring-amqpspring-rabbitrabbitmq-exchange

解决方案


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

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

rabbitmq-plugins 启用 rabbitmq_delayed_message_exchange

只是把它放在那里别人面对这个


推荐阅读