超轻量的定时器

项目中一个特殊要求,需要轻量的定时器程序,所以简单实现了一个:

核心执行器:
[code]
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();
}

}
[/code]

任务接口:
[code]
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;

}
[/code]

示例实现:
[code]
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;
}
}
[/code]

配置:
[code]
step=10000
times=-1
task/demo=com.**.timer.DemoTaskImpl
task/wiki_index=com.**.service.IndexTaskImpl
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值