Spring-Boot AMQP 1.3.1.RELEASE - 持久队列

时间:2016-01-22 15:43:42

标签: java spring rabbitmq amqp spring-amqp

由于某些原因,即使我在Spring AMQP Queue配置中指定了该设置,我的队列也不会创建为持久性:

@Bean
Queue queue() {
    //durable queue - true
    return new Queue(queueName, true);
}

我使用Spring AMQP连接到RabbitMQ并在Direct Exchange上侦听该队列。

@Bean
DirectExchange exchange() {
    return new DirectExchange(exchangeName);
}

@Bean
Binding binding(Queue queue, DirectExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(queueName);
}

@Bean
public ConnectionFactory connectionFactory() {

    CloudFactory cloudFactory = new CloudFactory();
    Cloud cloud = cloudFactory.getCloud();

    AmqpServiceInfo serviceInfo = (AmqpServiceInfo) 
            cloud.getServiceInfo(serviceName);

    CachingConnectionFactory connectionFactory =
        new CachingConnectionFactory(serviceInfo.getHost());
    connectionFactory.setUsername(serviceInfo.getUserName());
    connectionFactory.setPassword(serviceInfo.getPassword());
    connectionFactory.setVirtualHost(serviceInfo.getVirtualHost());
    return connectionFactory;
}

@Bean
MessageListenerAdapter underwritingMessageListener() throws Exception {
    return new MessageListenerAdapter(new UnderwritingMessageListener()) {{
        setDefaultListenerMethod("onMessage");
    }};
}

@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, 
        MessageListenerAdapter underwritingMessageListener) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(queueName);
    container.setMessageListener(underwritingMessageListener);
    return container;
}

我错过了配置步骤吗?

2 个答案:

答案 0 :(得分:2)

该设置未生效,因为在部署上述代码之前,Queue已经存在,非持久性。通过管理控制台或CLI删除队列,允许应用程序将队列声明为持久(重启后)。

如果在已经在不同状态下声明Queue而不是继续进行无效部署时,Spring-AMQP(RabbitMQ)引发异常会很有帮助。

答案 1 :(得分:0)

您需要RabbitAdmin @Bean来进行声明。

它将自己注册到连接工厂,并在建立连接时执行声明。