/* * DemoInterruptableJob.java */ package com; import java.util.Calendar; import org.quartz.InterruptableJob; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.UnableToInterruptJobException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * An implementation of an InterruptableJob *
* * @author thanooj kalathuru */ public class DemoInterruptableJob implements InterruptableJob { // logging services private static Logger _log = LoggerFactory.getLogger(DemoInterruptableJob.class); // job name private String _jobName = ""; private boolean isInterrupted = false; /** * * Empty constructor for job initialization *
*/ public DemoInterruptableJob() { } /** * * Called by the{@link org.quartz.Scheduler}
when a *{@link org.quartz.Trigger}
fires that is associated with the *Job
. *
* * @throws JobExecutionException * if there is an exception while executing the job. */ public void execute(JobExecutionContext context) throws JobExecutionException { int i = 0; try { _jobName = context.getJobDetail().getFullName(); _log.info("---- " + _jobName + " executing at " + Calendar.getInstance().getTime()); while (i < 1000000000L) { if(isInterrupted){ _log.info("- inside isInterrupted -"); throw new InterruptedException(); }else{ if(i%100000000L == 0) _log.info("---- i:: " + i); i++; } } _log.info("---- " + _jobName + " completing at " + Calendar.getInstance().getTime()); }catch (InterruptedException e) { _log.info("- isInterrupted at --- i:: " + i); return; } } @Override public void interrupt() throws UnableToInterruptJobException { _log.info("--INTERUPTING-- "); isInterrupted = true; } } *********************************** /* * InterruptExample.java * */ package com; import java.util.Iterator; import java.util.List; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class InterruptExample { private static Logger log = LoggerFactory.getLogger(InterruptExample.class); public void run() throws Exception { System.setProperty("org.quartz.scheduler.skipUpdateCheck", "true"); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler scheduler = sf.getScheduler(); JobDetail job = new JobDetail("interruptableJob1", "group1", DemoInterruptableJob.class); CronTrigger trigger = new CronTrigger(); trigger.setName("dummyTriggerName"); trigger.setCronExpression("0/7 * * * * ?"); scheduler.scheduleJob(job, trigger); scheduler.start(); try { @SuppressWarnings("unchecked") List<JobExecutionContext> jobsList = (List<JobExecutionContext>) scheduler .getCurrentlyExecutingJobs(); Iterator<JobExecutionContext> jobsIterator = jobsList .listIterator(); log.info("----------jobsList size: " + jobsList.size()); while (jobsIterator.hasNext()) { JobExecutionContext context = (JobExecutionContext) jobsIterator .next(); log.info("----------" + context.getJobDetail().getFullName()); } Thread.sleep(3000L); log.info("----****--- Started interrupt Scheduler -----------------"); scheduler.interrupt(job.getName(), job.getGroup()); log.info("----****--- ended interrupt Scheduler -----------------"); } catch (Exception e) { e.printStackTrace(); } SchedulerMetaData metaData = scheduler.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { InterruptExample example = new InterruptExample(); example.run(); } } ****************************************** 2013-05-23 17:05:06 INFO SimpleThreadPool:270 - Job execution threads will use class loader of thread: main 2013-05-23 17:05:06 INFO SchedulerSignalerImpl:60 - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2013-05-23 17:05:06 INFO QuartzScheduler:222 - Quartz Scheduler v.1.8.5 created. 2013-05-23 17:05:06 INFO RAMJobStore:139 - RAMJobStore initialized. 2013-05-23 17:05:06 INFO QuartzScheduler:244 - Scheduler meta-data: Quartz Scheduler (v1.8.5) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2013-05-23 17:05:06 INFO StdSchedulerFactory:1280 - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2013-05-23 17:05:06 INFO StdSchedulerFactory:1284 - Quartz scheduler version: 1.8.5 2013-05-23 17:05:06 INFO QuartzScheduler:500 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. 2013-05-23 17:05:06 INFO InterruptExample:42 - ----------jobsList size: 0 2013-05-23 17:05:07 DEBUG SimpleJobFactory:50 - Producing instance of Job 'group1.interruptableJob1', class=com.DemoInterruptableJob 2013-05-23 17:05:07 DEBUG JobRunShell:215 - Calling execute on job group1.interruptableJob1 2013-05-23 17:05:07 INFO DemoInterruptableJob:56 - ---- group1.interruptableJob1 executing at Thu May 23 17:05:07 IST 2013 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 0 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 100000000 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 200000000 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 300000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 400000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 500000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 600000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 700000000 2013-05-23 17:05:09 INFO InterruptExample:49 - ----****--- Started interrupt Scheduler ----------------- 2013-05-23 17:05:09 INFO DemoInterruptableJob:77 - --INTERUPTING-- 2013-05-23 17:05:09 INFO InterruptExample:51 - ----****--- ended interrupt Scheduler ----------------- 2013-05-23 17:05:09 INFO DemoInterruptableJob:60 - - inside isInterrupted - 2013-05-23 17:05:09 INFO InterruptExample:57 - Executed 1 jobs. 2013-05-23 17:05:09 INFO DemoInterruptableJob:70 - - isInterrupted at --- i:: 781135745 2013-05-23 17:05:14 DEBUG SimpleJobFactory:50 - Producing instance of Job 'group1.interruptableJob1', class=com.DemoInterruptableJob 2013-05-23 17:05:14 DEBUG JobRunShell:215 - Calling execute on job group1.interruptableJob1 2013-05-23 17:05:14 INFO DemoInterruptableJob:56 - ---- group1.interruptableJob1 executing at Thu May 23 17:05:14 IST 2013 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 0 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 100000000 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 200000000 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 300000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 400000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 500000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 600000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 700000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 800000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 900000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:68 - ---- group1.interruptableJob1 completing at Thu May 23 17:05:16 IST 2013
Thursday, May 23, 2013
Interrupt a Quartz's job using InterruptableJob
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment