小编典典

Spring Boot-非Web应用程序的长期运行的应用程序

spring-boot

我有一个简单的Spring-Boot应用程序,它仅使用AMQP依赖关系(只是'org.springframework.boot:spring-boot- starter-amqp'-例如,没有Web依赖关系,因此JAR中没有包含任何应用程序服务器)。

我想要的是让应用程序运行并侦听队列,并在收到消息时将一些信息记录到数据库中-
但是,由于没有应用程序服务器,因此一旦启动,它就会再次关闭(因为什么都没做)。是否有最佳方法来使此应用程序在侦听消息的同时运行?

代码中没有什么奇怪的,只有标准的应用程序配置,还有标有@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 )
    }
}

(我考虑过的一个选择只是分解一个计划的过程-仅是心跳之类的东西,无论如何都可能很适合进行监视-但是还有其他更好/标准的方法吗?)


阅读 758

收藏
2020-05-30

共1个答案

小编典典

您需要确保启动消息侦听器容器Bean,如示例中所示:

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