简体   繁体   中英

Spring-Boot AMQP 1.3.1.RELEASE - Durable Queue

For some reason my queue is not created as durable even though I specify that setting within the Spring AMQP Queue configuration:

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

I am the using Spring AMQP to connect to RabbitMQ and listen to that Queue on a 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;
}

Am I missing a configuration step?

The setting did not take effect because the Queue already existed, as non-durable, prior to deploying the code above. Deleting the Queue, via the management console or CLI, allowed the application to declare the Queue as durable (after reboot).

It would be helpful if Spring-AMQP (RabbitMQ) threw an exception when a Queue was already declared in a different state rather than proceeding with an invalid deployment.

You need a RabbitAdmin @Bean to do the declarations.

It registers itself with the connection factory and performs the declarations when the connection is established.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM