简易而不简单的定时任务框架 _JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 简易而不简单的定时任务框架

简易而不简单的定时任务框架

 2011/11/4 8:11:19  javacoo  http://javacoo.iteye.com  我要评论(0)
  • 摘要:今天在网上发现了这个定时任务框架,绝对的好东西啊,废话不多直接上代码了。整个框架就3个类:ScheduleIterator.javaimportjava.util.Date;publicinterfaceScheduleIterator{Datenext();}Scheduler.javaimportjava.util.Date;importjava.util.Timer;importjava.util.TimerTask;publicclassScheduler
  • 标签:
今天在网上发现了这个定时任务框架,绝对的好东西啊,废话不多直接上代码了。
整个框架就3个类:
ScheduleIterator.java

import java.util.Date;

public interface ScheduleIterator {
Date next();
}


Scheduler.java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Scheduler {
class SchedulerTimerTask extends TimerTask {
private SchedulerTask schedulerTask;
private ScheduleIterator iterator;

public SchedulerTimerTask(SchedulerTask schedulerTask,
ScheduleIterator iterator) {
this.schedulerTask = schedulerTask;
this.iterator = iterator;
}

public void run() {
schedulerTask.run();
reschedule(schedulerTask, iterator);
}
}

private final Timer timer = new Timer();

public Scheduler() {
}

public void cancel() {
timer.cancel();
}

public void schedule(SchedulerTask schedulerTask, ScheduleIterator iterator) {

Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.VIRGIN) {
throw new IllegalStateException("Task already scheduled or cancelled");
}
schedulerTask.state = SchedulerTask.SCHEDULED;
schedulerTask.timerTask = new SchedulerTimerTask(schedulerTask,
iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}

private void reschedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.CANCELLED) {
schedulerTask.timerTask = new SchedulerTimerTask(
schedulerTask, iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}
}

}

SchedulerTask.java

import java.util.TimerTask;

public abstract class SchedulerTask implements Runnable {
final Object lock = new Object();

int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;

TimerTask timerTask;

protected SchedulerTask() {
}

public abstract void run();

public boolean cancel() {
synchronized (lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}

public long scheduledExecutionTime() {
synchronized (lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}

}
========================================================================================================
以下是我自己写的一个 ScheduleIterator  实现类 以及一个测试类
SimpleScheduleIterator.java

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* 时间进度迭代器
* <li>返回 月/周/天/小时/分钟/秒 计划的下一次执行时间</li>
* @author javacoo
* @since 2011-11-03
*/
public class SimpleScheduleIterator implements ScheduleIterator {
/**每个月的第几周,每周的第几天,小时(24小时制),分钟,秒*/
private final Integer weekOfMonth,dayOfWeek,hourOfDay, minute, second;
private final Calendar calendar = Calendar.getInstance();

public SimpleScheduleIterator(Integer weekOfMonth,Integer dayOfWeek,Integer hourOfDay, Integer minute, Integer second) {
this(weekOfMonth,dayOfWeek,hourOfDay, minute, second, new Date());
}

public SimpleScheduleIterator(Integer weekOfMonth,Integer dayOfWeek,Integer hourOfDay, Integer minute, Integer second, Date date) {
this.weekOfMonth = weekOfMonth;
this.dayOfWeek = dayOfWeek;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
calendar.setTime(date);
if(null != weekOfMonth){
calendar.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
}
if(null != dayOfWeek){
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
}
if(null != hourOfDay){
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
}
if(null != minute){
calendar.set(Calendar.MINUTE, minute);
}
if(null != second){
calendar.set(Calendar.SECOND, second);
}
calendar.set(Calendar.MILLISECOND, 0);

if (!calendar.getTime().before(date)) {
if(null != weekOfMonth){
calendar.add(Calendar.MONTH, -1);
}else if(null != dayOfWeek){
calendar.add(Calendar.WEEK_OF_MONTH, -1);
}else if(null != hourOfDay){
calendar.add(Calendar.HOUR_OF_DAY, -1);
}else if(null != minute){
calendar.add(Calendar.MINUTE, -1);
}else if(null != second){
calendar.add(Calendar.SECOND, -1);
}
}
}

public Date next() {
if(null != weekOfMonth){
calendar.add(Calendar.MONTH, 1);
}else if(null != dayOfWeek){
calendar.add(Calendar.WEEK_OF_MONTH, 1);
}else if(null != hourOfDay){
calendar.add(Calendar.HOUR_OF_DAY, 1);
}else if(null != minute){
calendar.add(Calendar.MINUTE, 1);
}else if(null != second){
calendar.add(Calendar.SECOND, 1);
}
System.out.println("下次执行时间:"+calendar.getTime());
return calendar.getTime();
}

}

TestSchedule.java

/**
* 测试
* @author javacoo
* @since 2011-11-03
*/
public class TestSchedule {
private final Scheduler scheduler = new Scheduler();

private final Integer weekOfMonth, dayOfWeek, hourOfDay, minute, second;

public TestSchedule(Integer weekOfMonth,Integer dayOfWeek,Integer hourOfDay, Integer minute, Integer second) {
this.weekOfMonth = weekOfMonth;
this.dayOfWeek = dayOfWeek;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
}

public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
execute();
}

private void execute() {
System.out.println("任务执行");
}
}, new SimpleScheduleIterator(weekOfMonth,dayOfWeek,hourOfDay, minute, second));
}

public static void main(String[] args) {
/**
* 每个月的第一周的第5天的17点54分的 任务开始时秒数
* 执行效果:开始任务时 输出              下次执行时间:Thu Nov 03 17:54:55 CST 2011
                                 执行任务后 输出              任务执行
                                                                                   下次执行时间:Sat Dec 03 17:54:55 CST 2011
*/
TestSchedule test = new TestSchedule(1,5,17,54,null);
test.start();
}

}

=====================================================================================================

以下是整合到JEECMS采集器多线程版的代码片段


/**
* 开始执行采集任务
*/
public boolean start(Integer id) {
CmsAcquisition acqu = cmsAcquisitionMng.findById(id);
if (acqu == null || acqu.getStatus() == CmsAcquisition.START) {
return false;
}
TaskSchedulerManage taskManage = new TaskSchedulerManage(this,acqu,11,25,0);
taskManage.start();
return true;
}


private class TaskSchedulerManage {
private final Scheduler scheduler = new Scheduler();
private final int hourOfDay, minute, second;
private final MultiThreadAcquisitionSvcImpl multiThreadAcquisitionSvc;
private final CmsAcquisition acqu;
public TaskSchedulerManage(MultiThreadAcquisitionSvcImpl multiThreadAcquisitionSvc,CmsAcquisition acqu,int hourOfDay, int minute, int second) {
this.multiThreadAcquisitionSvc = multiThreadAcquisitionSvc;
this.acqu = acqu;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
}
public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
processer();
}
private void processer() {
System.out.println("============开始执行计划任务=================");
new Thread(new MainThreadProcesser(multiThreadAcquisitionSvc,acqu)).start();
}
}, new SimpleScheduleIterator(null,null,hourOfDay, minute, second));
}
}

很简单吧!!关键是 ScheduleIterator实现类了,框架有多强大 完全取决于你的ScheduleIterator实现类了。
  • 相关文章
发表评论
用户名: 匿名