Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧启动执行器关闭不';t终止无限循环_Java_Spring_Spring Boot_Shutdown_Spring Boot Actuator - Fatal编程技术网

Java 弹簧启动执行器关闭不';t终止无限循环

Java 弹簧启动执行器关闭不';t终止无限循环,java,spring,spring-boot,shutdown,spring-boot-actuator,Java,Spring,Spring Boot,Shutdown,Spring Boot Actuator,Im我的spring boot应用程序我有一个组件,其方法在下面的无限循环中运行一些作业,实际上它检查db中的一些数据: while(true) {// DOES SOME JOB} 以下是我的spring boot应用程序入口点: @SpringBootApplication public class Application implements CommandLineRunner { private final Service service; @Autowired

Im我的spring boot应用程序我有一个组件,其方法在下面的无限循环中运行一些作业,实际上它检查db中的一些数据:

while(true) {// DOES SOME JOB}
以下是我的spring boot应用程序入口点:

@SpringBootApplication
public class Application implements CommandLineRunner {

    private final Service service;

    @Autowired
    public Application(Service service) {
        this.service = service;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) {
        service.run();
    }
}
我启用了actuator的shutdown端点,所以我会通过:
curl-X POST localhost:8080/actuator/shutdown来终止应用程序,在我的例子中,它只终止spring上下文,但循环仍然运行。。。是否可以像
System.exit(0)
那样杀死整个应用程序(即使使用无限循环)


注意:我知道可以使用shutdown link编写自己的上下文感知端点,每当有人请求端点时,我可以关闭spring上下文并系统退出(0)(它肯定会终止llop)甚至为无限循环提供布尔标志,但是spring是否默认提供了一些功能,比如说更优雅的功能?

您可以使用@PreDestroy注释优雅地关闭应用程序

@PreDestroy
public void destroy() {
 System.out.println("destroy");
}

因此,当您使用ctrl+C终止应用程序时,循环也将终止。

您可以使用@PreDestroy annotation优雅地关闭应用程序

@PreDestroy
public void destroy() {
 System.out.println("destroy");
}
因此,当您使用ctrl+C终止应用程序时,循环也将终止。

对于执行器,其“关闭”方法将关闭应用程序上下文

虽然bean是由spring管理的,但是由于它的一些方法中有一个无限循环,spring不能真正知道这一点,也不能真正打破这个循环

Spring确实管理bean的生命周期,而且确实像@Shruti Gupta所说的那样,您可以创建一个带有
@PreDestroy
注释的方法,以便Spring调用它,但同样,您必须实现打破循环的逻辑

下面是一个可能适合您的示例:

@Component
public class MyBean {
    private boolean shouldKeepCheckingDB = true; // consider volatile as well if you need it, kind of out of scope for this question, but might be useful

    public void checkDB() {
        while(shouldKeepCheckingDB) { // instead of while(true)
            // check the database
        }
    }

    @PreDestroy
    void stop() {
        this.shouldKeepCheckingDB = false;
    } 
}
对于执行器,其“shutdown”方法关闭应用程序上下文

虽然bean是由spring管理的,但是由于它的一些方法中有一个无限循环,spring不能真正知道这一点,也不能真正打破这个循环

Spring确实管理bean的生命周期,而且确实像@Shruti Gupta所说的那样,您可以创建一个带有
@PreDestroy
注释的方法,以便Spring调用它,但同样,您必须实现打破循环的逻辑

下面是一个可能适合您的示例:

@Component
public class MyBean {
    private boolean shouldKeepCheckingDB = true; // consider volatile as well if you need it, kind of out of scope for this question, but might be useful

    public void checkDB() {
        while(shouldKeepCheckingDB) { // instead of while(true)
            // check the database
        }
    }

    @PreDestroy
    void stop() {
        this.shouldKeepCheckingDB = false;
    } 
}

为什么不用于循环
@Scheduler
?当您关闭spring上下文时,计划的方法将被终止。其中一些可能是您在application.properties中启用了shutdown选项了吗
endpoints.shutdown.enabled=true
@Arnaud-Claudel是的,当然。端点可用,并且它关闭了上下文,但循环仍在运行。我的意思是,不要在
中使用无限循环,而(){}
使用无限
@Scheduler
对于您的任务可能是更好的选择。为什么不在循环
@Scheduler
中使用呢?当您关闭spring上下文时,计划的方法将被终止。其中一些可能是您在application.properties中启用了shutdown选项了吗
endpoints.shutdown.enabled=true
@Arnaud-Claudel是的,当然。端点是可用的,它关闭了上下文,但循环仍在运行。我的意思是,不要使用带有
while(){}的无限循环
对于您的任务,使用infinite
@Scheduler
可能是一个更好的选择。我无法使用ctrl+C,因为在不同的端口上有多个应用程序的运行实例,我需要发送http请求以终止它@PreDestroy与System.exit(0)配合使用已经足够好了,但我现在会避免使用它,至少在我有更好的解决方案之前我不能使用ctrl+C,因为在不同的端口上有多个应用程序的运行实例,我需要发送http请求来终止它@PreDestroy与System.exit(0)结合使用已经足够好了,但我现在会避免使用它,至少在我找到更好的解决方案之前是的,这会起作用,但不幸的是,我发现在循环中我一直在向db读取SQL查询,我的意思是我有一个必须选择的表列表,并且我在列表上迭代,在嵌套循环(oops-legacy)中读取大量表。。。。因此,这种方法需要在几个地方(我有循环的地方)放置布尔标志,但我希望有一个地方可以终止应用程序…好吧,legacy-我感觉到你的痛苦:)但我不认为如果没有某种条件,是否继续或停止它将起作用,对不起。甚至重构到线程并从外部停止它在Java中都被认为是一种不好的做法。是的,这会起作用,但不幸的是,我发现在循环中我一直在向db读取SQL查询,我的意思是我有一个表列表我必须选择,我在列表上迭代,在嵌套循环中读取很多表(oops-legacy)。。。。因此,这种方法需要在几个地方(我有循环的地方)放置布尔标志,但我希望有一个地方可以终止应用程序…好吧,legacy-我感觉到你的痛苦:)但我不认为如果没有某种条件,是否继续或停止它将起作用,对不起。甚至重构到线程并从外部阻止它在Java中也被认为是一种糟糕的做法。