0

Is it possible to load spring context with @RabbitListener even if RabbitMQ message broker is down? The behavior should be similar as in case of broker disconnection. Application is waiting for broker and when it is restored, then listerner is automaticaly reconnected.

Spring Boot 1.3.2.RELEASE

GitHub demo project

spring-amqp configuration:

@Bean
public MessageConverter jsonMessageConverter() {
    return new JsonMessageConverter();
}

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

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setConcurrentConsumers(10);
    factory.setMaxConcurrentConsumers(10);
    factory.setMessageConverter(jsonMessageConverter());
    return factory;
}

@Bean
public AmqpAdmin amqpAdmin() {
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
    rabbitAdmin.setIgnoreDeclarationExceptions(true); // useless
    return rabbitAdmin;
}

listener configuration:

@Service
public class CalculatorServiceV2 {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "calc_service_v2.multiply", durable = "false", autoDelete = "true"),
            exchange = @Exchange(value = "calc_service_v2", durable = "false", autoDelete = "true"),
            key = "multiply"))
    public Result multiply(Operands operands) {
        // do something..
    }
}
deii
  • 113
  • 8

1 Answers1

0

What version of Spring AMQP are you using?

Can you provide a stack trace? (edit the question, don't try to put it in a comment).

I just ran a test and it works as expected; the listener container attempts to reconnect every 5 seconds (the default recovery interval).

rabbitAdmin.setIgnoreDeclarationExceptions(true); // useless

In 1.6 (currently at milestone 1) we've changed that boolean to skip all exceptions; previously it only applied to exceptions caused by errors returned from the broker.

However, the admin won't attempt to declare the elements until the connection is established so that should be moot in this context.

I need to see a stack trace to understand what's going on in your case.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • After some experiments I found, that I was impatient. With five listeners and ten workers (concurrent consumers) it takes approximately 4 minutes to finish all attempts to connect to broker. Then the spring context is finally loaded. I can post an example project to GitHub if you like. – deii Feb 29 '16 at 11:31
  • If you don't mind - I just tried to reproduce with Boot 1.3.3 and the app context loaded immediately, even with the broker down. – Gary Russell Feb 29 '16 at 14:40
  • I just tried the demo project with Boot 1.3.3: Started Application in 193.104 seconds – deii Mar 02 '16 at 17:11
  • Hmmm - I guess I need to see your project. – Gary Russell Mar 02 '16 at 18:02
  • Folow link in the question: [Github demo project](https://github.com/deii/spring-amqp-context-loading) – deii Mar 03 '16 at 11:07
  • I downloaded your project; unzipped it; stopped rabbit, and it started in 1.004 seconds. [Gist here](https://gist.github.com/garyrussell/117b179d21d740ea91f6). I can't imagine what could be different between our environments. Perhaps you could post your log in a gist? – Gary Russell Mar 03 '16 at 14:00
  • Log from run on my env: [Gist](https://gist.github.com/deii/f07510e4bbabfc9ad6e5) – deii Mar 04 '16 at 08:58
  • Weird; I just ran it with the boot runner (previously I ran from the IDE) and got the same result; updated [my gist](https://gist.github.com/garyrussell/117b179d21d740ea91f6). Perhaps the next step is to enable DEBUG logging. Perhaps you have some network issue? – Gary Russell Mar 04 '16 at 13:40