Created
October 28, 2011 19:00
-
-
Save abstractj/1323104 to your computer and use it in GitHub Desktop.
SimpleJob
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.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.Date; | |
/** | |
* 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 { | |
Trigger trigger = new SimpleTrigger("mySimpleTrigger", Scheduler.DEFAULT_GROUP, new Date(), null, 0, 1000L); | |
JobDetail jobDetail = new JobDetail(); | |
jobDetail.setName("sJobs"); | |
jobDetail.setGroup("sGroup"); | |
jobDetail.setJobClass(WatchDogJob.class); | |
jobDetail.getJobDataMap().put("currentJobContext", context); | |
context.getScheduler().scheduleJob(jobDetail, trigger); | |
} | |
} |
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.Job; | |
import org.quartz.JobExecutionContext; | |
import org.quartz.JobExecutionException; | |
import org.quartz.UnableToInterruptJobException; | |
/** | |
* User: Bruno | |
* Date: 10/28/11 | |
* Time: 3:45 PM | |
*/ | |
public class WatchDogJob implements Job { | |
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { | |
System.out.println("WatchDogJob executing"); | |
JobExecutionContext currentJobContext = (JobExecutionContext)jobExecutionContext.getJobDetail().getJobDataMap().get("currentJobContext"); | |
try { | |
currentJobContext.getScheduler().interrupt(currentJobContext.getJobDetail().getName(), currentJobContext.getJobDetail().getGroup()); | |
} catch (UnableToInterruptJobException e) { | |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | |
} | |
} | |
} |
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 15 16 * * ?"; | |
JobDetail jobDetail = new JobDetail(); | |
jobDetail.setGroup(group); | |
jobDetail.setName(jobName); | |
jobDetail.setDescription(description); | |
jobDetail.setJobClass(SimpleJob.class); | |
jobDetail.setRequestsRecovery(true); | |
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