【问题标题】:Spring Boot - long running application for non-web appSpring Boot - 非 Web 应用程序的长时间运行应用程序
【发布时间】:2015-11-01 22:28:26
【问题描述】:

我有一个简单的 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 )
    }
}

(我考虑过的一个选择是启动一个预定的进程——只是一个心跳或其他东西,无论如何这可能对监控很有利——但是还有其他更好/标准的方法吗?)

【问题讨论】:

  • 你在启动 MessageListenerContainer bean 吗?
  • @ᴳᵁᴵᴰᴼ ahhh,是的,你是对的..这只是我的马虎 - 它甚至在 SpringBoot AMQP 入门示例中有 MessageListenerContainer - 应该对我自己的问题投反对票!如果您想将其作为答案,我将标记为正确(否则将在今晚晚些时候添加带有详细信息的答案)

标签: java spring spring-boot


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2020-10-26
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多