超軽量タイマー

5301 ワード

プロジェクトの特殊な要件は、軽量なタイマプログラムが必要であるため、簡単に実現できます.
コアエフェクタ:

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