如何让Spring RabbitMQ创建一个新的Queue?

时间:2013-05-04 05:31:19

标签: java spring rabbitmq amqp

在使用rabbit-mq的(有限)经验中,如果为尚不存在的队列创建新的侦听器,则会自动创建队列。我正在尝试使用带有rabbit-mq的Spring AMQP项目来设置一个监听器,而我正在收到错误。这是我的xml配置:

<rabbit:connection-factory id="rabbitConnectionFactory" host="172.16.45.1" username="test" password="password" />

<rabbit:listener-container connection-factory="rabbitConnectionFactory"  >
    <rabbit:listener ref="testQueueListener" queue-names="test" />
</rabbit:listener-container>

<bean id="testQueueListener" class="com.levelsbeyond.rabbit.TestQueueListener"> 
</bean>

我在RabbitMq日志中得到了这个:

=ERROR REPORT==== 3-May-2013::23:17:24 ===
connection <0.1652.0>, channel 1 - soft error:
{amqp_error,not_found,"no queue 'test' in vhost '/'",'queue.declare'}

来自AMQP的类似错误:

2013-05-03 23:17:24,059 ERROR [org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] (SimpleAsyncTaskExecutor-1) - Consumer received fatal exception on startup
org.springframework.amqp.rabbit.listener.FatalListenerStartupException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.

从堆栈跟踪看来,队列是以“被动”模式创建的 - 任何人都可以指出我是如何创建不使用被动模式的队列所以我没有看到这个错误?或者我错过了其他什么?

4 个答案:

答案 0 :(得分:15)

较旧的帖子,但这仍然在Google上显得相当高,所以这里有一些更新的信息:

2015年11月23日

Spring 4.2.x 以及Spring-Messaging和 Spring-Amqp 1.4.5.RELEASE Spring-Rabbit 1.4.5.RELEASE ,通过@Configuration类的一些注释声明交换,队列和绑定变得非常简单:

@EnableRabbit
@Configuration
@PropertySources({
    @PropertySource("classpath:rabbitMq.properties")
})
public class RabbitMqConfig {    
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqConfig.class);

    @Value("${rabbitmq.host}")
    private String host;

    @Value("${rabbitmq.port:5672}")
    private int port;

    @Value("${rabbitmq.username}")
    private String username;

    @Value("${rabbitmq.password}")
    private String password;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);

        logger.info("Creating connection factory with: " + username + "@" + host + ":" + port);

        return connectionFactory;
    }

    /**
     * Required for executing adminstration functions against an AMQP Broker
     */
    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    /**
     * This queue will be declared. This means it will be created if it does not exist. Once declared, you can do something
     * like the following:
     * 
     * @RabbitListener(queues = "#{@myDurableQueue}")
     * @Transactional
     * public void handleMyDurableQueueMessage(CustomDurableDto myMessage) {
     *    // Anything you want! This can also return a non-void which will queue it back in to the queue attached to @RabbitListener
     * }
     */
    @Bean
    public Queue myDurableQueue() {
        // This queue has the following properties:
        // name: my_durable
        // durable: true
        // exclusive: false
        // auto_delete: false
        return new Queue("my_durable", true, false, false);
    }

    /**
     * The following is a complete declaration of an exchange, a queue and a exchange-queue binding
     */
    @Bean
    public TopicExchange emailExchange() {
        return new TopicExchange("email", true, false);
    }

    @Bean
    public Queue inboundEmailQueue() {
        return new Queue("email_inbound", true, false, false);
    }

    @Bean
    public Binding inboundEmailExchangeBinding() {
        // Important part is the routing key -- this is just an example
        return BindingBuilder.bind(inboundEmailQueue()).to(emailExchange()).with("from.*");
    }
}

要提供帮助的一些资料和文档:

  1. Spring annotations
  2. Declaring/configuration RabbitMQ for queue/binding support
  3. Direct exchange binding (for when routing key doesn't matter)
  4. 注意:看起来我错过了一个版本 - 从 Spring AMQP 1.5 开始,事情变得更加容易,因为您可以在听众处声明完整绑定! / p>

答案 1 :(得分:9)

似乎解决了我的问题是添加管理员。这是我的xml:

<rabbit:listener-container connection-factory="rabbitConnectionFactory"  >
    <rabbit:listener ref="orderQueueListener" queues="test.order" />
</rabbit:listener-container>

<rabbit:queue name="test.order"></rabbit:queue>

<rabbit:admin id="amqpAdmin" connection-factory="rabbitConnectionFactory"/>

<bean id="orderQueueListener" class="com.levelsbeyond.rabbit.OrderQueueListener">   
</bean>

答案 2 :(得分:4)

您可以在连接标记之后但在侦听器之前添加此内容:

<rabbit:queue name="test" auto-delete="true" durable="false" passive="false" />

不幸的是,根据XSD架构,被动属性(如上所列)无效。但是,在我看到的每个queue_declare实现中,passive都是一个有效的queue_declare参数。我很想知道这是否有效,或者他们是否打算将来支持它。

以下是队列声明的完整选项列表: http://www.rabbitmq.com/amqp-0-9-1-reference.html#class.queue

这里是spring rabbit schema的完整XSD(包含注释): http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd

答案 3 :(得分:0)

Spring Boot 2.1.6 Spring AMQP 2.1.7 开始,您可以在启动期间创建队列(如果此队列不存在):

@Component
public class QueueConfig {

    private AmqpAdmin amqpAdmin;

    public QueueConfig(AmqpAdmin amqpAdmin) {
        this.amqpAdmin = amqpAdmin;
    }

    @PostConstruct
    public void createQueues() {
        amqpAdmin.declareQueue(new Queue("queue_one", true));
        amqpAdmin.declareQueue(new Queue("queue_two", true));
    }
}