Spring AMQP - 应用程序启动时未创建 RabbitMQ 连接 [英] Spring AMQP - RabbitMQ connection is not created on application startup

查看:72
本文介绍了Spring AMQP - 应用程序启动时未创建 RabbitMQ 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 应用程序,我的目标是在应用程序启动时声明队列、交换和绑定.应用程序将向各种队列生成消息,应用程序上将没有消费者.

I have a Spring Boot application and my goal is to declare queues, exchanges, and bindings on application startup. The application will produce messages to various queues there will be no consumers on the application.

我已经在我的 pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>2.2.12.RELEASE</version>
</dependency>

我的配置类

@Configuration
public class RabbitConfiguration {

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("myhost", 5672);
        connectionFactory.setUsername("example_name");
        connectionFactory.setPassword("example_pass");
        return connectionFactory;
    }

    @Bean
    public AmqpAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public Queue declareQueue() {
        return new Queue("test_queue", true, false, false);
    }

    @Bean
    public DirectExchange declareDirectExchange() {
        return new DirectExchange("test_direct_exchange", true, false);
    }

    @Bean
    public Declarables declareBindings() {

        return new Declarables(
            new Binding("test_queue", DestinationType.QUEUE, "test_direct_exchange", "test_routing_key", null)
        );
    }
}

我的问题是在应用程序启动时没有创建队列、交换和绑定.Spring Boot 甚至没有打开连接.连接、队列等仅在我向队列生成消息时创建.

My problem is that queues, exchanges, and bindings are not created on the application startup. Spring boot does not even open the connection. The connection, queues, etc are created only when I produce messages to the queues.

推荐答案

如果你想在应用启动时强制声明并且没有任何消费者,你可以将执行器启动器添加到类路径,或者简单地创建共享自己连接.

If you want to force declaration during app startup and don't have any consumers, you can either add the actuator starter to the classpath, or simply create the shared connection yourself.

@Bean
ApplicationRunner runner(ConnectionFactory cf) {
    return args -> cf.createConnection().close();
}

这不会关闭连接;如果您想这样做,请调用 cf.resetConnection().

This won't close the connection; if you want to do that, call cf.resetConnection().

如果您希望应用在代理关闭时启动,请执行以下操作.

If you want the app to start if the broker is down, do something like this.

@Bean
ApplicationRunner runner(ConnectionFactory cf) {
    return args -> {
        boolean open = false;
        while(!open) {
            try {
                cf.createConnection().close();
                open = true;
            }
            catch (Exception e) {
                Thread.sleep(5000);
            }
        }
    };
}

这篇关于Spring AMQP - 应用程序启动时未创建 RabbitMQ 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆