Quartz2.2.1フレームワークの簡単な手順
SimpleExampleクラス:
HelloJobクラス:
QuartJobは、タスクを実行するJavaクラスです.
このクラスで実装org.quartz.Jobインタフェースは、execute(JobExecutionContext context)メソッドを書き換え、Jobが実行する操作は、このメソッドに書かれています.
public class SimpleExample {
private static Log logger = LogFactory.getLog(SimpleExample.class);
public static void main(String[] args) {
SimpleExample example=new SimpleExample();
example.runScheduler();
}
public void runScheduler() {
try {
// scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
logger.info("scheduler start up...");
// JobDetail
String jobName = "HelloJob";
String jobGroup = Scheduler.DEFAULT_GROUP;
Class jobClass = HelloJob.class;
JobDetail job = new JobDetailImpl(jobName, jobGroup, jobClass);
// JobDataMap Job
job.getJobDataMap().put("name", "Joe Ma");
job.getJobDataMap().put("age", "18");
//
String TriggerName = "triggerTest";//
int repeatCount = 5;//
long repeatInterval = 2 * 1000;// 2
SimpleTrigger trigger = new SimpleTriggerImpl(TriggerName, repeatCount,
repeatInterval);
// CronTrigger( 8 )
// try {
// CronTrigger cTrigger = new CronTriggerImpl("cronTrigger",
// Scheduler.DEFAULT_GROUP, "0 0/5 8 ? * MON,THUR");
//
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//jobListener
// JobListener jobListener=new HelloJobListener();
//TriggerListener
// TriggerListener triggerListener=new HelloTriggerListener();
//Scheduler
// SchedulerListener schedulerListener=new HelloSchedulerListener();
// job
scheduler.scheduleJob(job, trigger);
// jobListener
// scheduler.getListenerManager().addJobListener(jobListener);
// TriggerListener
// scheduler.getListenerManager().addTriggerListener(triggerListener);
// SchedulerListenet
// scheduler.getListenerManager().addSchedulerListener(schedulerList//ener);
// scheduler(scheduler )
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (SchedulerException e) {
// TODO Auto-generated catch block
logger.error(e);
}
}
}
HelloJobクラス:
QuartJobは、タスクを実行するJavaクラスです.
このクラスで実装org.quartz.Jobインタフェースは、execute(JobExecutionContext context)メソッドを書き換え、Jobが実行する操作は、このメソッドに書かれています.
public class HelloJob implements Job{
private static Log logger=LogFactory.getLog(HelloJob.class);
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("job is starting ...");
JobDetail jobDetail = context.getJobDetail();
logger.info("fired at ---" + context.getFireTime());
logger.info("Next fire time ----" + context.getNextFireTime());
// job scheduler JobDataMap
JobDataMap map =jobDetail.getJobDataMap();
Iterator iterator=map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object value=map.get(key);
logger.info("key= "+key+" , value= "+value);
}
}
}