服务完成后关闭服务

时间:2019-08-02 11:12:55

标签: java spring spring-boot spring-batch

我正在创建一个监视文件的服务,一旦文件可用,它就会将其加载到db。完成这项工作后,我想正常关闭该应用程序。

但是当我使用context.close时,它会引发异常(尽管该应用程序已关闭),但我想将其关闭而不引起任何异常。

@profile("non-test")
class Manager implements ApplicationContextAware{

@PostConstruct
public void init()
{
    //watch for file and as soon as file  is available invokde trigger()
}

public void trigger()
{
    //load to db
    shutodown()
}

public void shutdown()
{
    SpringApplication.exit(context);
}
}

当我叫关机时,它抛出以下异常。 AnnotationConfigApplicationContext已关闭

我希望该应用正常关闭,没有任何异常。

1 个答案:

答案 0 :(得分:0)

您的问题很可能是Spring依赖项的存在,这会使您的应用程序寿命长。这种依赖例如是spring-boot-starter-web。此启动程序包括Servlet容器,因为依赖关系和Spring默认将永远运行。

如果此类路径中没有此类依赖项,那么您的Spring Boot应用程序将自动关闭(无需任何特殊的措施来杀死它)。我建议您看一下Spring guides for batch job中的一些内容。

相关问题