Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

475
Vistas
Programmatically enable and disable certain @RabbitListener's in Spring?

I have a class A that publishes event E1. E1 is consumed by class B in the same application that is annotated with @RabbitListener. B does some things and then publishes event E2 that is consumed by C etc etc (forming a process chain).

What I want to do is two things:

  1. I want to test A in an integration test but while doing so I'd like to disable the RabbitListener's so that the entire process that is the result of E1 being published is not executed. I only want to assert that A does what it's supposed to and publishes E1. I have managed to accommodate this by setting spring.rabbitmq.listener.auto-startup=false.
  2. I also want to test B in an integration test by publishing E1 to RabbitMQ so that I can be confident that I've configured B's RabbitListerner correctly. But again I don't want C to be called as a side-effect of E2 being published.

I know I can probably do this using mocks but preferably I'd like to test the real deal and using the actual components (including sending the message to an actual RabbitMQ instance that in my case is running in Docker).

Can I achieve this in a nice way in Spring Boot? Or is it perhaps recommended to use @RabbitListenerTest and indeed use mocks?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The @RabbitListener has id property:

/**
 * The unique identifier of the container managing for this endpoint.
 * <p>If none is specified an auto-generated one is provided.
 * @return the {@code id} for the container managing for this endpoint.
 * @see org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry#getListenerContainer(String)
 */
String id() default "";

And that RabbitListenerEndpointRegistry#getListenerContainer(String) returns MessageListenerContainer and there you already can control start()/stop() of individual @RabbitListener handler.

about 4 years ago · Santiago Trujillo Denunciar

0

A follow-up idea to the accepted answer is to have some sort of abstract BaseAmqpIntegrationTest which does the following:

public abstract class BaseAmqpIntegrationTest {

    @Autowired
    protected RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;

    @BeforeEach
    protected void setUpBeforeEach() {
        rabbitListenerEndpointRegistry.getListenerContainers()
                                      .forEach(Lifecycle::stop);

        getRequiredListenersToStart().forEach(listener -> rabbitListenerEndpointRegistry.getListenerContainer(listener)
                                                                                        .start());
    }

    protected abstract List<String> getRequiredListenersToStart();

}

This makes it reusable and ensures that all @RabbitListeners are disabled "by default" and requires each test to explicitly enable the listener(s) that it tests. The test sub classes can then simply override getRequiredListenersToStart() to provide the IDs of the @RabbitListeners which they require.

PS: Cleaning it up would of course also work:

public abstract class BaseAmqpIntegrationTest {

    @AfterEach
    protected void cleanUpAfterEach() {
        rabbitListenerEndpointRegistry.getListenerContainers()
                                      .forEach(Lifecycle::stop);
    }

}

Or a bit more fine-grained:

public abstract class BaseAmqpIntegrationTest {

    @AfterEach
    protected void cleanUpAfterEach() {
        getRequiredListenersToStart().forEach(listener -> rabbitListenerEndpointRegistry.getListenerContainer(listener)
                                                                                        .stop());
    }

}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda