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

问题描述

我希望我的队列绑定 2 个不同的交换,下面的方法不起作用

配置文件-

@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-plugins 启用 rabbitmq_delayed_message_exchange

只要把它拿出来就有别人面对这个

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...