Spring: How to Monitor Quartz Job from controller?

14,739

Solution 1

You can easily retrieve your job trigger state

example for quartz 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

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

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

Solution 2

Use jwatch its really easy to configure.And it also provide a Restful-api which 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

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.

Solution 3

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:

I received the object of Scheduler from context:

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

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.

Thanks all

Share:
14,739

Related videos on Youtube

A Gupta
Author by

A Gupta

JavaScript Engineer. Mostly working on the ReactJS. Also worked on NodeJS, Python and Java in past.

Updated on September 15, 2022

Comments

  • A Gupta
    A Gupta over 1 year

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

    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 JobListener and 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 Controller so that I can monitor both job in my 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 ?

  • A Gupta
    A Gupta almost 11 years
    TriggerKey is not present in Spring and Quartz. I am using Quartz 1.8.x.
  • willome
    willome almost 11 years
    You are right : org.quartz.TriggerKey is present on quartz 2.x and not on 1.8
  • A Gupta
    A Gupta almost 11 years
    is there any alternative way by using 1.8 ? I cannot update to 2.x. I have searched through API still did not find any solution to it