2007-12-26
超轻量的定时器
关键字: 定时器 timer 轻量
项目中一个特殊要求,需要轻量的定时器程序,所以简单实现了一个:
核心执行器:
任务接口:
示例实现:
配置:
核心执行器:
public class TaskExcuter {
public static final TaskExcuter instance = new TaskExcuter();
private List tasks = new ArrayList();
private long step;
private long times;
private void init() throws Exception {
Properties config = new Properties();
config.load(TaskExcuter.class.getResourceAsStream("timer.config"));
step = Long.parseLong(config.getProperty("step"));
times = Long.parseLong(config.getProperty("times"));
Enumeration names = config.propertyNames();
while (names.hasMoreElements()) {
String name = ((String) names.nextElement()).trim();
if (name.toLowerCase().startsWith("task")) {
String clazz = config.getProperty(name);
try {
ITask task = (ITask) Class.forName(clazz).newInstance();
task.setName(name.split("/")[1]);
tasks.add(task);
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
private void start() {
new Thread(new Runnable() {
public void run() {
long i = 0;
while (i <= times||times==-1) {
try {
Thread.sleep(step);
}
catch (InterruptedException e) {
e.printStackTrace();
}
Date taskTimer = new Date();
for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
ITask task = (ITask) iterator.next();
task.setTaskTimer(taskTimer);
boolean state = task.willExcute();
if (state) {
log("开始执行计划任务["+i+1+"]:" + task.getName());
try {
boolean ex = task.excute();
if (ex) {
log("[" + task.getName() + "]执行成功~!");
} else {
log("[" + task.getName() + "]执行失败~!");
}
}
catch (TaskException e) {
e.printStackTrace();
log("[" + task.getName() + "]执行失败~!" + e);
}
}
}
System.gc();
i++;
}
}
}).start();
}
private SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
private void log(String msg) {
Date d = new Date();
msg = ft.format(d) + "-->>" + msg;
System.out.println(msg);
}
public static void main(String[] args) throws Exception {
TaskExcuter.instance.init();
TaskExcuter.instance.start();
}
}
任务接口:
public abstract class ITask {
private String name;
private Date TaskTimer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTaskTimer() {
return TaskTimer;
}
public void setTaskTimer(Date taskTimer) {
TaskTimer = taskTimer;
}
public abstract boolean willExcute();
public abstract boolean excute() throws TaskException;
}
示例实现:
public class DemoTaskImpl extends ITask {
public boolean willExcute() {
Date d = getTaskTimer();
// if (d.getDate() == 20) {
// return true;
// }
return true;
}
public boolean excute() throws TaskException {
System.out.println("-------------ok");
return true;
}
}
配置:
step=10000 times=-1 task/demo=com.**.timer.DemoTaskImpl task/wiki_index=com.**.service.IndexTaskImpl
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 6698 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
翀儿
共 4 张
共 4 张
最新评论
-
JBPM与SPRING事务整合之深 ...
这是因为通过spring的类路径加载hbm方式在weblogic下有问题,无法正 ...
-- by betafox -
JBPM与SPRING事务整合之深 ...
你不觉得第一步很愚蠢,用jbpm还要去把jar包解压缩
-- by jinguizi -
JBPM与SPRING事务整合之深 ...
能把你的这几个配置文件件完整的贴出来参考一下吗?
-- by yuyanshan -
实现Microsoft Project 文 ...
我需要把数据库中的数据导出生成mpp文件,用mpxj,请问怎么实现啊邮箱chen ...
-- by chenhui_19850710@sina.com -
JBPM与SPRING事务整合之深 ...
留下脚印 日后备查
-- by isky






评论排行榜