分类:
2008-10-28 18:05:08
public void init(ActionServlet actionServlet,
ModuleConfig moduleConfig) {
System.out.println("Initializing Scheduler PlugIn for Jobs!");
// Retrieve the ServletContext
// 获取ServletContext
ServletContext ctx = actionServlet.getServletContext();
// The Quartz Scheduler
// Quartz Scheduler对象
Scheduler scheduler = null;
// Retrieve the factory from the ServletContext.
// It will be put there by the Quartz Servlet
// 从ServletContext取得由Quartz Servlet放置在此的factory对象。
StdSchedulerFactory factory = (StdSchedulerFactory)
ctx.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY);
try{
// Retrieve the scheduler from the factory
// 从factory取得scheduler
scheduler = factory.getScheduler();
// Start the scheduler in case, it isn't started yet
// 如果scheduler尚未启动,则启动它
if (m_startOnLoad != null &&
m_startOnLoad.equals(Boolean.TRUE.toString())){
System.out.println("Scheduler Will start in " +
m_startupDelayString + " milliseconds!");
//wait the specified amount of time before
// starting the process.
// 在启动之前等待指定长度的时间
Thread delayedScheduler =
new Thread(new DelayedSchedulerStarted (
scheduler, m_startupDelay));
//give the scheduler a name. All good code needs a name
//给任务调度器命名。好的代码总该有名字!
delayedScheduler.setName("Delayed_Scheduler");
//Start out scheduler
//启动任务调度器
delayedScheduler.start();
}
} catch (Exception e){
e.printStackTrace();
}
sm_scheduler = scheduler;
}
QuartzInitializer
Quartz Initializer Servlet
org.quartz.ee.servlet.QuartzInitializerServlet
1
shutdown-on-unload
true
start-scheduler-on-load
false
Trigger trigger = new CronTrigger("trigger1", "group1");
trigger.setCronExpression("0 0 15 ? * WED");
JobDetail jobDetail =
new JobDetail("Hello World Job",
"Hello World Group",
HelloWorld.class,
true, true, true);
//Schedule The work
//调度这项任务
SchedulerPlugIn.scheduleWork(scheduledJobDetail, trigger);
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
//extend the proper Quartz class
//继承适当的Quartz类
public class HelloWorld extends Job {
//override the execute method
//覆盖execute方法
public void execute(JobExecutionContext context) {
// Every job has it's own job detail
//每个Job都有独立的JobDetail
JobDetail jobDetail = context.getJobDetail();
// The name is defined in the job definition
//name在Job定义中指定
String jobName = jobDetail.getName();
//Every job has a Job Data map for storing extra information
//每个Job都有一个Job Data map来存放额外的信息
JobDataMap dataMap = jobDetail.getJobDataMap();
System.out.println("Hello World!!!");
}
}
Trigger trigger = new SimpleTrigger("trigger1", "group1");
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(10000L); // milliseconds毫秒
Trigger trigger = TriggerUtils.makeDailyTrigger(2, 22);
trigger.setName("trigger1");
trigger.setGroup("group1");
Trigger trigger = new CronTrigger("trigger1", "group1");
trigger.setCronExpression("0 22 2 * * ?");
每5秒执行一次的触发器
/* *
* 方法一:makeSecondlyTrigger
* 注意以下代码将创建一个立即启动的触发器。要控制启动时间,使用
* trigger.setStartTime(Date)方法。
*/
Trigger trigger = TriggerUtils.makeSecondlyTrigger(5);
trigger.setName("MyFiveSecondTrigger");
trigger.setGroup("MyTriggerGroup");
/*
*
* 方法二:设置SimpleTrigger的重复次数和间隔时间。
* 注意以下代码将创建一个立即启动的触发器。要控制启动时间,使用
* trigger.setStartTime(Date)方法。
*/
Trigger trigger = new SimpleTrigger("trigger1", "group1");
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(5000L); // milliseconds
Trigger trigger = new SimpleTrigger("trigger1", "group1");
// 24 hours * 60(minutes per hour) *
// 60(seconds per minute) * 1000(milliseconds per second)
// 24小时 * 60(分钟每小时) * 60(秒每分钟)* 1000(毫秒每秒钟)
trigger.setRepeatInterval(24L * 60L * 60L * 1000L);