Created
October 28, 2011 18:57
-
-
Save abstractj/1323090 to your computer and use it in GitHub Desktop.
WatchDogSchedulerFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abstractj.jobs; | |
import org.quartz.*; | |
import org.quartz.core.JobRunShell; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* User: Bruno | |
* Date: 10/28/11 | |
* Time: 9:59 AM | |
*/ | |
public class SimpleJob implements Job, InterruptableJob { | |
private final Logger LOGGER = LoggerFactory.getLogger(SimpleJob.class); | |
public void execute(JobExecutionContext context) throws JobExecutionException { | |
LOGGER.info("Started Job Execution at " + java.util.Calendar.getInstance().getTime()); | |
try { | |
System.out.println("SimpleJob started execution"); | |
watchDog(context); | |
Thread.sleep(100000000000000000L); | |
System.out.println("SimpleJob was executed"); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (SchedulerException e) { | |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | |
} | |
LOGGER.info("Finished Job Execution at " + java.util.Calendar.getInstance().getTime()); | |
System.out.println("Last call"); | |
} | |
public void interrupt() throws UnableToInterruptJobException { | |
System.out.println("Job Interrompido"); | |
} | |
private void watchDog(final JobExecutionContext context) throws SchedulerException { | |
ScheduledExecutorService s = Executors.newScheduledThreadPool(1); | |
Long timeout = (Long)context.getJobDetail().getJobDataMap().get("timeout"); | |
s.schedule(new Runnable() { | |
public void run() { | |
try { | |
System.out.println("Executing scheduler"); | |
context.getScheduler().interrupt(context.getJobDetail().getName(), context.getJobDetail().getGroup()); | |
} catch (UnableToInterruptJobException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, timeout, TimeUnit.MILLISECONDS); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abstractj.scheduler; | |
import com.abstractj.jobs.SimpleJob; | |
import org.quartz.*; | |
import org.quartz.impl.StdSchedulerFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* User: Bruno | |
* Date: 10/5/11 | |
* Time: 8:52 PM | |
*/ | |
public class WatchDogSchedulerFactory { | |
private static final Logger LOGGER = LoggerFactory.getLogger(WatchDogSchedulerFactory.class); | |
private static SchedulerFactory sf = new StdSchedulerFactory(); | |
private static Scheduler scheduler; | |
public static void createScheduler() throws SchedulerException, Exception { | |
scheduler = sf.getScheduler(); | |
try { | |
String group = "group1"; | |
String jobName = "job1"; | |
String description = "description1"; | |
String triggerName = "trigger1"; | |
String cronExpression = "0 48 16 * * ?"; | |
JobDetail jobDetail = new JobDetail(); | |
jobDetail.setGroup(group); | |
jobDetail.setName(jobName); | |
jobDetail.setDescription(description); | |
jobDetail.setJobClass(SimpleJob.class); | |
jobDetail.setRequestsRecovery(true); | |
jobDetail.getJobDataMap().put("timeout", 1000L); | |
CronTrigger trigger = new CronTrigger(triggerName, group, cronExpression); | |
scheduler.scheduleJob(jobDetail, trigger); | |
scheduler.start(); | |
} catch (Exception ex) { | |
LOGGER.error("Parser error", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment