Search code examples
springspring-bootmessaging

Spring boot start on listening to messages on application start


I have a Spring Boot application that starts listening on Azure IOT Hub at application start. It is done this way:

@EventListener
public void subscribeEventMessages(ContextRefreshedEvent event)  {
    client
            .receive(false) // set this to false to read only the newly available events
            .subscribe(this::hubAllEventsCallback);
}

My problem is, that this uses ContextRefreshedEvent but in fact i only want to start it once on application start. I also checked other methods how start something at the beginning, like CommandLineRunner.

On the other hand if implementing listeners for more standard stuff like JMS there are specific Annotations like @JmsListener or providing Beans of specific Types.

My question is: Can i leverage some of these more message(subscribe) related mechanisms to start my method?


Solution

  • If we don't want our @EventListener to listen on "context refresh" but only on "context start", please (try) replace:

    ...which is "sibling class" with exactly this semantical difference.