java Spring:如何从控制器监控 Quartz 作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17446066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:01:52  来源:igfitidea点击:

Spring: How to Monitor Quartz Job from controller?

javaspringspring-mvcquartz-scheduler

提问by agpt

I have created two jobs in Spring project which run at two different time independent to each other.

我在 Spring 项目中创建了两个作业,它们在彼此独立的两个不同时间运行。

public class JobA extends QuartzJobBean
{
    @Override
    protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException 
    {
      // my actual work
    }
}

and

public class JobB extends QuartzJobBean
{
    @Override
    protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException 
    {
      // my actual work
    }
}

both are running fine at given time, but I need to provide some monitor functionality through which we can check whether jobs are running or not.
I came across JobListenerand have seen other resources too but getting confused at the time of its implementation. I am not getting exactly how to use this listener in Spring Controllerso that I can monitor both job in my jsp.

两者在给定时间都运行良好,但我需要提供一些监视器功能,通过它我们可以检查作业是否正在运行。
我遇到了JobListener并且也看到了其他资源,但在实施时感到困惑。我不知道如何在Spring Controller 中使用这个监听器,以便我可以在我的 jsp 中监控这两个工作。

Update: I am using Quartz 1.8. How to check if any job is halted ? Is there any way we can restart any halted or broken job ?

更新:我使用的是 Quartz 1.8。如何检查是否有任何作业被暂停?有什么办法可以重新启动任何停止或中断的工作?

回答by willome

You can easily retrieve your job trigger state

您可以轻松检索作业触发状态

example for quartz 2.x :

石英 2.x 的示例:

// get the scheduler factory bean from the spring context
Scheduler scheduler = (Scheduler) getApplicationContext().getBean("schedulerFactoryBean");
// get the TriggerKey 
TriggerKey triggerKey = TriggerKey.triggerKey("serviceCronTrigger");
// get the state from the triggerKey
TriggerState triggerState = scheduler.getTriggerState(triggerKey); 

For quartz 1.8

对于石英 1.8

According to the API docs, Scheduler.getTriggerState(String triggerName, String triggerGroup)can tell you the state of a particular trigger, returning one of these constants: Trigger.STATE_NORMAL, Trigger.STATE_PAUSED, Trigger.STATE_COMPLETE, Trigger.STATE_ERROR, Trigger.STATE_BLOCKED, Trigger.STATE_NONE

根据 API 文档,Scheduler.getTriggerState(String triggerName, String triggerGroup)可以告诉您特定触发器的状态,返回以下常量之一:Trigger.STATE_NORMAL, Trigger.STATE_PAUSED, Trigger.STATE_COMPLETE, Trigger.STATE_ERROR, Trigger.STATE_BLOCKED, Trigger.STATE_NONE

 // get the scheduler factory bean from the spring context
 Scheduler scheduler = (Scheduler)   getApplicationContext().getBean("schedulerFactoryBean");
 // get the state 
 int state = scheduler.getTriggerState(triggerName, triggerGroup);

回答by Dangling Piyush

Use jwatchits really easy to configure.And it also provide a Restful-apiwhich will return you all the information about jobs and schedulers in JSON format so that you could easily parse it and display on jsp page.A sample url for monitoring all job instances will be like

使用jwatch真的很容易配置。它还提供了一个Restful-api,它将以JSON格式返回有关作业和调度程序的所有信息,以便您可以轻松解析它并在jsp页面上显示。用于监视所有作业的示例网址实例会像

http://localhost:8081/jwatch/ui?action=monitor_jobs

And the response is pretty simple:

响应非常简单:

data: [
    {
      calendarName: "",
      fireTime: "06/30/11 15:59:01 EDT",
      jobGroup: "group0",
      jobName: "j_1",
      jobRunTime: 0,
      nextFireTime: "06/30/11 16:00:01 EDT",
      previousFireTime: "06/30/11 15:58:01 EDT",
      quartzInstanceId: "f5c1edd6-0101-4c93-9162-58ca104b8fdb",
      recovering: false,
      refireCount: 0,
      scheduledFireTime: "06/30/11 15:59:01 EDT",
      schedulerId: "MEGA",
      schedulerName: "MegaScheduler",
      triggerGroup: "group0",
      triggerName: "t_1"
    },
    {
      calendarName: "",
      fireTime: "06/30/11 15:59:01 EDT",
      jobGroup: "group1",
      jobName: "j_1",
      jobRunTime: 0,
      nextFireTime: "06/30/11 16:00:01 EDT",
      previousFireTime: "06/30/11 15:58:01 EDT",
      quartzInstanceId: "f5c1edd6-0101-4c93-9162-58ca104b8fdb",
      recovering: false,
      refireCount: 0,
      scheduledFireTime: "06/30/11 15:59:01 EDT",
      schedulerId: "MEGA",
      schedulerName: "MegaScheduler",
      triggerGroup: "group1",
      triggerName: "t_1"
    },...

Soources : click here.

来源:点击这里

回答by agpt

I just did some trial and error with Quartz 1.8.6 and with the reference from @willome answer, I would like to put my working code here:

我只是对 Quartz 1.8.6 进行了一些试验和错误,并参考了@willome 的回答,我想把我的工作代码放在这里:

I received the object of Scheduler from context:

我从上下文中收到了 Scheduler 的对象:

Scheduler scheduler = (Scheduler) SpringContextService.getBean(context,"scheduler");

Scheduler scheduler = (Scheduler) SpringContextService.getBean(context,"scheduler");

Then by using scheduler object, I get trigger object of name I mentioned in quartz configuration:

然后通过使用调度程序对象,我得到了我在石英配置中提到的名称的触发器对象:

Trigger cronTrigger = scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);

Trigger cronTrigger = scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP);

and via trigger object (its an Abstract class) I get getPreviousFireTime();and getNextFireTime();as per my cron expression, and using ajax I get the latest update on my choice of time interval by calling spring controller.

并通过触发器对象(它是一个抽象类)我得到getPreviousFireTime();getNextFireTime();根据我的 cron 表达式,并使用 ajax 我通过调用 spring 控制器获得我选择的时间间隔的最新更新。

Thanks all

谢谢大家