quartz是opensymphony提供的用来做SCHEDULE的框架
在spring中通过org.springframework.scheduling.quartz.SchedulerFactoryBean来获得Schedule类
1 2 3 4 5 6 7 8 9 10 11 12
| <bean id="quartzSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" scope="singleton">
<!-- set quartz scheduler factory configuration location -->
<!-- <property name="configLocation" value="" /> -->
<!-- 系统会在启动时加载 -->
<property name="autoStartup" value="true" />
</bean>
<bean id="taskManage" class="com.sumit.task.TaskManage" scope="singleton">
<property name="taskDAO" ref="taskDao" />
<property name="scheduler" ref="quartzSchedulerFactory" />
</bean>
|
QUARTZ中有Job与Trigger两种概念
一.Job详细描述具体的任务运行代码实现
1.新增一个任务到scheduler
1 2 3 4 5 6
| JobDetail job = new JobDetail(taskName, Scheduler.DEFAULT_GROUP,DefaultQuartzJob.class);
JobDataMap jobDataMap = job.getJobDataMap();
//根据PUT不同的TASK而运行不同的任务
jobDataMap.put("Task", task);
job.setDurability(true);
scheduler.addJob(job, true);
|
==>DefaultQuartzJob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| publicclass DefaultQuartzJob implements Job {
privatestatic Logger logger = Logger.getLogger(DefaultQuartzJob.class);
publicvoid execute(JobExecutionContext ctx) throws JobExecutionException {
JobDataMap jobDataMap = ctx.getJobDetail().getJobDataMap();
TaskTO task = (TaskTO) jobDataMap.get("Task");
Task entryTask = task.getEntryTask();
if (entryTask == null) {
logger
.error("Task " + task.getName()
+ " couldn't be initialized!");
return;
}
TaskContext taskContext = new DefaultTaskContext();
// 将触发任务的Trigger中所有的参数添加到上下文中
JobDataMap triggerDataMap = ctx.getTrigger().getJobDataMap();
String[] keys = triggerDataMap.getKeys();
if (keys != null) {for (int i = 0; i < keys.length; i++) {
Object obj = triggerDataMap.get(keys[i]);
if (obj instanceof String[]) {
String [] data=(String[]) obj;
if(data.length>0){
taskContext.setParameter(keys[i], data[0]);
}}elseif(obj instanceof String){
taskContext.setParameter(keys[i], (String)obj);
}}}try{// 为任务时间执行次数加1
TaskManage tm = (TaskManage) triggerDataMap
.get(TaskManage.TASKMANAGE_ATTRIBUTE_NAME);
tm.addTriggerExecuteTimes(triggerDataMap
.getString(TaskManage.TRIGGER_ATTRIBUTE_NAME));
}catch (TaskException e) {
logger.error(
"catch exception while process task " + task.getName(), e);
}try{//根据实例化不同的TASK而运行不同的任务
entryTask.process(taskContext);
}catch (TaskException e) {
logger.error(
"catch exception while process task " + task.getName(), e);
}}} |
==>TASK接口,继承它可以实现不同的任务实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| /**
* 定时任务的基本接口
*/publicinterface Task
{
/**
* 任务执行
*
* @param request
* 请求参数
* @param response
* 返回参数
* @param cxt
* @roseuid 4535938002AC
*/publicvoid process(TaskContext cxt) throws TaskException;
/**
* 任务名称
*
* @return java.lang.String
* @roseuid 453DB0AB01B5
*/public String getName();
} |
2.从scheduler中删除一个任务
1
| scheduler.deleteJob(taskName, Scheduler.DEFAULT_GROUP);
|
二.Trigger定时器,任务与定时器是一对多的关系
1.新建Trigger,初步分为CRONTRIGGER,SIMPLETRIGGER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| if (TRIGGER_CRON.equals(triggerType)) {//创建CRONTRIGGER
trigger = new CronTrigger(triggerName, Scheduler.DEFAULT_GROUP,
taskName, Scheduler.DEFAULT_GROUP, validStartTime,
validEndTime, triggerPattern);
trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
}else{//创建SIMPLETRIGGER
trigger = new SimpleTrigger(triggerName,
Scheduler.DEFAULT_GROUP, taskName,
Scheduler.DEFAULT_GROUP, validStartTime, validEndTime,
repeatCount, repeatInterval);
trigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT);
}
try{//添加trigger到scheduler
scheduler.scheduleJob(trigger);
logger.debug("trigger " + trigger.getName()
+ " have been loaded to scheduler!");
}catch (SchedulerException e) {
logger.error("Catch exception " + e.getMessage()
+ " while adding trigger " + triggerName
+ " into scheduler", e);
thrownew TaskException("0096", "Catch exception " + e.getMessage()
+ " while adding trigger " + triggerName
+ " into scheduler", e);
}
|
2.删除Trigger
1 2 3 4 5 6 7 8 9 10
| try{
scheduler.unscheduleJob(triggerName, Scheduler.DEFAULT_GROUP);
}catch (SchedulerException e) {
logger.error("Catch exception " + e.getMessage()
+ " while remove trigger " + triggerName
+ " from scheduler", e);
thrownew TaskException("0096", "Catch exception " + e.getMessage()
+ " while removing trigger " + triggerName
+ " from scheduler", e);
|
posted on 2007-01-12 19:32
☜♥☞MengChuChen 阅读(2120)
评论(0) 编辑 收藏 所属分类:
Spring