`
cheshuai
  • 浏览: 33936 次
文章分类
社区版块
存档分类
最新评论

Butterfly Scheduling 一个轻量级的调度工具

阅读更多

在JAVA 的世界里,提到调度工具基本都想到Quartz,今天介绍一个更轻量级的调度工具:Butterfly Scheduling ,Jar包非常小只有20几k,在项目中添加jenkov-butterfly-scheduling-1.3.9-beta.jar,然后就可以测试一下了

 

一个最简单的实例:

SchedulerTest.java

 

public class SchedulerTest {

    public static void main(String[] args) {
        SchedulerService schedulerService = new SchedulerService();


        String url = "http://jenkov.com/rss/rss_all.jsp";
        UrlTask urlTask = new UrlTask(url);
        String commandLine = "help ";
        CommandLineTask clTask = new CommandLineTask(commandLine);

        IScheduledTaskListener listener = new MyTaskListener();

        schedulerService.addScheduledTaskListener(listener);

        long timeInterval = 10 * 1000; //one分钟 in milliseconds.
        schedulerService.addIntervalScheduledTask(clTask, timeInterval);
        schedulerService.addIntervalScheduledTask(urlTask, timeInterval);
//        SchedulerService scheduler = new SchedulerService();
//        schedulerService.addDailyScheduledTask(urlTask, 3, 30, 0);
//        schedulerService.addDailyScheduledTask(clTask, 3, 30, 0);

//Run in current thread
        schedulerService.run();


    }

}

 

 

MyTaskListener.java

 

public class MyTaskListener implements IScheduledTaskListener {
    public void taskScheduled(ScheduledTaskContext scheduledTaskContext) {
        System.out.println("Start :" + scheduledTaskContext.getScheduledTask().getTaskName());
    }

    public void taskFailureScheduled(ScheduledTaskContext scheduledTaskContext) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void taskExecuted(ScheduledTaskContext scheduledTaskContext) {

//get execution result history
        List<ScheduledTaskExecutionResult> results =
                scheduledTaskContext.getExecutionResults();
//get latest execution result
        ScheduledTaskExecutionResult latestResult = results.get(0);
//see if the result was success or not

        boolean success = latestResult.getStatus() == ScheduledTaskExecutionResult.SUCCESS;
        boolean error = latestResult.getStatus() == ScheduledTaskExecutionResult.ERROR;

        System.out.println("end :" + scheduledTaskContext.getScheduledTask().getTaskName());
    }
}

  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics