如何让春天的RabbitMQ来创建一个新的队列?(How to get Spring RabbitM

2019-09-01 23:25发布

与兔MQ我的(有限)的经验,如果你尚不存在的队列创建一个新的侦听器,会自动创建队列。 我试图使用Spring AMQP项目,兔MQ建立一个侦听器,我发现了一个错误信息。 这是我的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.

它会从堆栈跟踪似乎队列是获得创建“被动”模式 - 任何人都可以指出我怎么会不使用被动模式,所以我没有看到这个错误创建队列? 还是我失去了什么东西?

Answer 1:

旧的线程,但仍显示了相当高的谷歌,所以这里的一些新的信息:

2015年11月23日

既然Spring 4.2.x版与Spring-消息和Spring AMQP 1.4.5.RELEASESpring兔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注解
  2. 声明/配置的RabbitMQ队列/绑定支持
  3. 直接交换结合(因为当路由关键无所谓)

:貌似我错过了一个版本-从春AMQP 1.5,事情就变得甚至你可以在声明听众充分结合正确的更轻松!



Answer 2:

什么似乎解决我的问题是添加一个管理员。 这里是我的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>


Answer 3:

您可以将连接标记后添加此,但听众面前:

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

不幸的是,根据XSD模式,无源属性(上面列出)是无效的。 然而,在每一个queue_declare实现我所看到的,被动的一直是有效的queue_declare参数。 我很好奇,看看是否可以将工作或他们是否打算支持它的未来。

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

这里是春兔架构完整的XSD(带有注释包括): http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd



文章来源: How to get Spring RabbitMQ to create a new Queue?