Java – spring: how to monitor quartz jobs from the controller?
I created two jobs in the spring project, which run independently of each other at two different times
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 run normally at a given time, but I need to provide some monitoring functions through which we can check whether the job is running I met joblistener and saw other resources, but I was confused when I implemented it I don't know exactly how to use this listener in spring controller so that I can monitor these two jobs in my JSP
Update: I'm using quartz 1.8 How do I check if any work has stopped? How can we restart any work that is stopped or destroyed?
Solution
You can easily retrieve the job trigger status
Quartz 2 Example of 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 documentation, scheduler Gettriggerstate (string triggername, string triggergroup) can tell you the status of a specific trigger and return one of the following 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);