2016-07-25 48 views
1

我想以这种方式安排我的工作: 1)只有一个线程运行它。 (系列) 2)在两次运行之间有固定间隔Spring @Scheduled fixedDelay无法正常工作

我在我的方法上面使用@Scheduled(fixedDelay = 10000),在我的applicationContext.xml中使用<task:annotation-driven/>。但根据我的印刷资料,两个作业之间的间隔不是我设定的10000毫秒。这是为什么?

19:07-25 22:38:46.190 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457526190 
20:07-25 22:38:48.191 INFO schedule:45 [21-thread-1] - [job#1] use 2000ms 
21:07-25 22:38:48.191 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457528191 
25:07-25 22:39:03.198 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457543198 
26:07-25 22:39:05.201 INFO schedule:45 [21-thread-1] - [job#1] use 2002ms 
27:07-25 22:39:05.202 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457545202 
31:07-25 22:39:20.205 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457560205 
32:07-25 22:39:22.209 INFO schedule:45 [21-thread-1] - [job#1] use 2004ms 
33:07-25 22:39:22.210 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457562210 
37:07-25 22:39:37.213 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457577213 
38:07-25 22:39:39.215 INFO schedule:45 [21-thread-1] - [job#1] use 2002ms 
39:07-25 22:39:39.215 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457579215 
43:07-25 22:39:54.221 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457594221 
44:07-25 22:39:56.225 INFO schedule:45 [21-thread-1] - [job#1] use 2004ms 
45:07-25 22:39:56.225 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457596225 
49:07-25 22:40:11.525 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457611525 
50:07-25 22:40:13.528 INFO schedule:45 [21-thread-1] - [job#1] use 2003ms 
51:07-25 22:40:13.529 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457613529 
55:07-25 22:40:28.237 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457628237 

你可以看到完成和运行之间的时间不是10000ms。 (更像15000)

的代码是(春季4.2.1):

@Scheduled(fixedDelay = 10000) 
    public void timerJob1(){ 
     SCHEDULER.info("[job#1] is running. -----> {}", System.currentTimeMillis()); 
     Stopwatch stopwatch = Stopwatch.createStarted(); 

     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
      long use = stopwatch.elapsed(TimeUnit.MILLISECONDS); 
      SCHEDULER.info("[timeJob1] use {}ms", use); 
     } 

     long use = stopwatch.elapsed(TimeUnit.MILLISECONDS); 
     SCHEDULER.info("[job#1] use {}ms", use); 

     SCHEDULER.info("[job#1] is finished. -----> {}", System.currentTimeMillis()); 
    } 

回答

0

您可能对线程池争被使用。创建自己的执行程序,通过实现和连接ScheduledConfigr来处理这些固定的延迟任务。

在您的configureTasks()实现中,将您自己的Executor设置为方法参数ScheduledTaskRegistrar上的调度程序。