spring amqp-我的spring应用程序是否可以将多个Exchange绑定到每个队列?

b5buobof  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(348)

我有两个不同的交换,我想我的队列绑定,下面的方法不起作用
配置文件-

@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个exchange(myexchange)绑定时,它工作得很好,但当我使用2个exchange运行此应用程序时,我会得到以下日志,最终它无法连接。请帮助我理解如何解决这个问题

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
v440hwme

v440hwme1#

我意识到这是因为我还没有启用插件。
我需要先启用这个插件,然后它才能工作。
rabbitmq插件支持rabbitmq\u延迟消息\u交换
只是把它放在那里,还有其他人面对这件事吗

相关问题