简体   繁体   中英

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' - eg 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?)

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;
}

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