JMX Timer与Java util timer和ScheduledThreadPoolExecutor

时间:2015-03-09 17:24:16

标签: java timer jmx weblogic12c

主要区别是b / t ScheduledThreadPoolExecutor vs java.util.Timer vs javax.management.timer.Timer?一个人比另一个人有什么好处?在服务器启动后立即开始每天运行两个作业的最佳设计是什么?

我见过的一个例子是使用JMX Timer扩展并附加了NotificationListener

public class TestNotificationListener implements NotificationListener {
    javax.management.timer.Timer timer = new Timer();

    public void init() {
        Calendar calendar = Calendar.getInstance();
        Date date1 = calendar.getTime();
        Date date2 = calendar.getTime();

        timer.addNotificationListener(this, null, "some handback object");

        int job1Id = timer.addNotification("type1", "Order", this, date1, 10000);
        int job2Id = timer.addNotification("type2", "Inventory ", this, date2, 10000);

        timer.start();
    }
    public void handleNotification(Notification notif, Object handback) {
        ...
    }
}

另一个例子直接使用Timer ...

public class TestNotificationListener extends ApplicationLifecycleListener {

    public void postStart(final ApplicationLifecycleEvent evt) {
        java.util.Timer Timer1 = new Timer();       
        Timer1.schedule(new Task1(), 10000);

        java.util.Timer Timer2 = new Timer();
        final SocialMediaCachingTask smcCacheTask = new SocialMediaCachingTask();
        Timer2.schedule(new Task2(), 10000);
    }
}

最后我看到了使用ScheduledThreadPoolExecutor

public class TestNotificationListener extends ApplicationLifecycleListener {

    public void postStart(final ApplicationLifecycleEvent evt) {
        Scheduler = new ScheduledThreadPoolExecutor(1);
        Scheduler.schedule(new Task1(Scheduler), 10000, TimeUnit.SECONDS);
        Scheduler2 = new ScheduledThreadPoolExecutor(1);
        Scheduler2.schedule(new Task2(Scheduler2), 10000, TimeUnit.SECONDS);
    }
}

0 个答案:

没有答案