[英]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.