Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: How to Monitor Quartz Job from controller?

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 ?

like image 215
A Gupta Avatar asked Dec 11 '22 13:12

A Gupta


1 Answers

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);
like image 176
willome Avatar answered Dec 14 '22 03:12

willome