La libreria Quartz: verificare il fatto che un Job sia già in esecuzione all’interno del Job stesso

Un esempio molto conciso:

public class EmptyJob implements Job {
 
    public void execute(JobExecutionContext context)
     throws JobExecutionException {
 
    	try {
			if (isJobRunning(context)) {
				logger.debug("Il job è già in esecuzione.");
				return;
			}
		} catch (SchedulerException e1) {
			throw new JobExecutionException("Impossibile statibilise se il JOB è in esecuzione");
		}
 
		System.out.println("Sono un Job con la J maiuscola");
 
    }
 
    private boolean isJobRunning(JobExecutionContext ctx) throws SchedulerException
	{
		List<JobExecutionContext> jobs = ctx.getScheduler().getCurrentlyExecutingJobs();
 
		for (JobExecutionContext job : jobs)
		{
			if (job.getJobDetail().getJobClass().getName().equals(this.getClass().getName()) && !job.getFireTime().equals(ctx.getFireTime()))
			{
				return true;
			}
		}
 
		return false;
	}
 
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>