avalon
avalon

Reputation: 2291

java quartz stop schedule after first exception

I have a quartz job:

public class GlobalIdleMonitorJob extends AbstractJob {

    @Override public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
            getJobService().processUserIdle();
        }catch(Exception e){
            getLogger().error("GlobalIdleMonitorJob",e);
        }
    }
}

This job runs every X seconds. I want this job to stop after first failure. In other words, if

getJobService().processUserIdle();

threw an exception, I want this job not run anymore, only after manual restart or web app server restart. Should I throw a special exception in catch block or how to configure it?

Upvotes: 2

Views: 1449

Answers (2)

Rodrigo Villalba Zayas
Rodrigo Villalba Zayas

Reputation: 5616

In your catch block you can unschedule a job like this:

try {
   getJobService().processUserIdle();
} catch(Exception e) {
   scheduler.unscheduleJob(triggerKey) 
   getLogger().error("GlobalIdleMonitorJob",e);
}

If you don't have any other trigger or job in your app, you can simply call scheduler.clear(), but this could not be a good idea if you are likely to add new jobs or triggers in the future.

Upvotes: 0

user2290411
user2290411

Reputation: 56

You can just set the setUnscheduleAllTriggers method of JobExecutionException class object to true(true). Refer the below attached example from the official quartz website for you reference.

--documentation

Upvotes: 1

Related Questions