Spring:如何从控制器监视Quartz Job?

时间:2013-07-03 10:52:21

标签: java spring spring-mvc quartz-scheduler

我在Spring项目中创建了两个作业,这两个作业在两个不同的时间相互独立运行。

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

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

两者在给定时间都运行良好,但我需要提供一些监视器功能,通过它我们可以检查作业是否正在运行。
我遇到JobListener并且看到了其他资源,但在实施时却感到困惑。我没有准确地在 Spring Controller 中使用这个监听器,这样我就可以在jsp中监视这两个作业。

更新: 我正在使用Quartz 1.8。如何检查是否有任何工作停止?我们有什么方法可以重新开始任何暂停或破坏的工作吗?

3 个答案:

答案 0 :(得分:9)

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

石英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); 

对于石英1.8

根据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);

答案 1 :(得分:3)

使用jwatch它非常容易配置。它还提供了一个Restful-api,它将以JSON格式返回有关作业和调度程序的所有信息,以便您可以轻松地解析它并在jsp上显示page.用于监控所有作业实例的示例网址将类似于

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

响应非常简单:

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

答案 2 :(得分:1)

我刚刚使用Quartz 1.8.6进行了一些试验和错误以及来自@willome的参考答案,我想把我的工作代码放在这里:

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

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

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

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

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

全部谢谢