Spring Boot - 用于非 Web 应用程序的长时间运行的应用程序 [英] Spring Boot - long running application for non-web app

查看:45
本文介绍了Spring Boot - 用于非 Web 应用程序的长时间运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Spring-Boot 应用程序,它只使用 AMQP 依赖项(只是 'org.springframework.boot:spring-boot-starter-amqp' - 例如没有网络依赖项,所以没有应用服务器包含在 JAR 中).

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?

代码中没有什么奇怪的,只是标准的应用程序配置,然后还有一个用@RabbitListener 标记的类

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

推荐答案

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

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

这篇关于Spring Boot - 用于非 Web 应用程序的长时间运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆