简体   繁体   English

我的应用程序停止时该如何捕捉?

[英]How can I catch when my application stops?

I hava a spring-boot application and I'm using quartz to create jobs. 我有一个spring-boot应用程序,正在使用石英创建作业。 I run my application like ./bin/app start I need to catch in my class that implements Job when my application stops like: ./bin/app stop Does anybody know how can I do that? 我像./bin/app start这样运行我的应用程序,当我的应用程序停止时,我需要赶上实现Job的类: ./bin/app stop有人知道我该怎么做吗? I tried to implement DisposableBean using the method destroy() but doesn't work. 我尝试使用方法destroy()实现DisposableBean ,但不起作用。 My main class in something like this: 我的主要班级是这样的:

public class Main implements CommandLineRunner {
    public static void main(String[] args) throws Exception {
       .....

The Quartz create jobs and in my job create a variable "lock" like this: Quartz创建作业,然后在我的作业中创建一个变量“ lock”,如下所示:

public class MyJob implements Job, DisposableBean {
private RLock lock = redissonClient.getFairLock(jobName);
    .....

    @Override
    public void destroy() throws Exception {
        if(lock != null) {
            lock.forceUnlock();
        }
    }

I need force the unlock when my application stop in the middle of execution so I need catch the stop in MyJob class not in the Main class. 当我的应用程序在执行过程中停止时,我需要强制解锁,因此我需要在MyJob类而不是Main类中捕获停止。 Thank you in advance!! 先感谢您!!

You should be able to attach an event observer to the application context, for example: 您应该能够将事件观察器附加到应用程序上下文,例如:

  public class MyJob implements Job, DisposableBean {

      public MyJob(ApplicationContext context) {
        ((ConfigurableApplicationContext)context).addApplicationListener(
           (ContextClosedEvent event) -> onContextClosed()
        );
      }

      private void onContextClosed() {
         ...
      }
  }

Use the @PreDestroy annotation. 使用@PreDestroy批注。 Annotate the method with @PreDestroy which you want to execute on stop. @PreDestroy注释要在停止时执行的方法。

@PreDestroy
public void onExit() {
   //STOP FROM THE LIFECYCLE
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的应用程序启动时停止 - my application stops when I start it 当我按回按钮时,我的应用程序停止工作 - When I press the button back, my application stops working 尝试保存文件时我的应用程序停止 - My application stops when I try to save a file 当我点击“注册”按钮时,我的应用程序停止并退出 - My Application Stops and Exits when I hit the Register button 当我更改活动时,我的计时器停止计时。 我该如何预防呢? - My chronometer stops when i change the activity. How can i prevent this? 为什么通过插件启动Java应用程序并退出该应用程序时,Eclipse停止运行,为什么我的Eclipse框架停止工作 - Why my eclipse framework stops working when through a plugin a java application starts and when i exit this application the Eclipse stops 如何在 OutOfMemoryError 使我的应用程序崩溃之前捕获它? - How do I catch an OutOfMemoryError before it crashes my application? 当我从Android应用程序请求数据时,我的网站停止工作 - My website stops working when I request data from my Android application 当我尝试切换到 MainActivity 时应用程序停止 - The application stops when I try to switch to MainActivity 如何在我的框架中捕获连接请求? - How can I catch connection requests in my framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM