rhinds
rhinds

Reputation: 10043

Spring Boot - long running application for non-web app

I have a simple Spring-Boot application that just uses the AMQP dependency (just 'org.springframework.boot:spring-boot-starter-amqp' - e.g. no web dependencies so no app server being included in the JAR).

All I want is for the application to run and listen to a queue and log some info to the DB whenever a message is recieved - however, as there is no application server, as soon as it starts up it just shuts down again (as there is nothing being done). Is there a best way to keep this application running whilst listening for messages?

There is nothing surprising in the code, just the standard application config, then also a class marked with @RabbitListener

@SpringBootApplication
class PersistenceSeriveApplication {

    static void main(String[] args) {
        SpringApplication.run PersistenceSeriveApplication, args
    }
}

@Configuration
@EnableRabbit
class QueueConfiguration {

    @Bean public Queue applicationPersistenceQueue( @Value( '${amqp.queues.persistence}' ) String queueName ) {
        new Queue( queueName )
    }
}

(One option I considered was just spinning up a scheduled process - just a heartbeat or something, which would probably be nice for monitoring anyway - but is there any other better/standard way?)

Upvotes: 4

Views: 3045

Answers (1)

guido
guido

Reputation: 19194

You need to make sure to start the message listener container bean, like shown in the examples:

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

Upvotes: 2

Related Questions