论坛首页 Java企业应用论坛

超轻量的定时器

浏览 2061 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-26  
项目中一个特殊要求,需要轻量的定时器程序,所以简单实现了一个:

核心执行器:
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
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics