Spring调度程序无法正常工作

时间:2017-06-19 10:57:49

标签: java spring

Weblogic 12上有一个Spring项目。 在项目中,很少有像

这样的弹簧调度程序
@Component
public class ExampleScheduler {

    @Autowired
    ExampleService exampleService;

    @Scheduled(fixedDelay = 1000)
    private void run(){
        exampleService.doSomething();
    }    
}

计划程序的设置:

<task:annotation-driven executor="executor" scheduler="scheduler"/>
<task:executor id="executor" pool-size="20"/>
<task:scheduler id="scheduler" pool-size="40"/>

使用@Configuration在课堂上进行@EnableScheduling。

问题是fixedDelay正常工作两次,然后在迭代之间暂停变为1.5分钟。 我在Scheduled注释中尝试过fixedRate或cron,但它没有帮助。

计划任务中的方法工作如100ms,项目有足够的内存,但调度程序运行缓慢。

2 个答案:

答案 0 :(得分:0)

fixedDelay argument is designed so that the timer for the next task starts after the previous finishes. i.e. If your task took .5 seconds to run then it'll effectively repeat ev0ery 1.5 seconds.

p.s. so fixedDelay would be best to avoid multiple processes from trying to beat each other and causing a race condition. For example, you have a scheduled task to update some values, and this process can take anywhere from .1 sec to 5 sec but you want to refresh every second. You'll use fixedDelay to avoid having multiple threads trying to complete the same task at the same time where the task that is using old data finishes last thus could erase the correct values from the newer tasks.

fixedRate measures from the start of each process. And would be this straight forward:

@Scheduled(fixedRate=500)

Because you need it every half-second, cron wouldn't be the optimal choice because it can only specify to the second, but if you needed to it would look like this, with 6 input spots and zone being optional:

@Scheduled(zone = "EST", cron = "* * * * * *") 

For the spring document link.

答案 1 :(得分:0)

你的班级必须至少有这些注释:

package org.springframework.scheduling.annotation

@Configuration
@EnableScheduling

您可以使用fixedDelay

进行设置
@Scheduled(fixedDelay = 1000)

还有initialDelay:

  @Scheduled(fixedDelay = 1000, initialDelay = 1000)

或使用fixedRate(每次执行任务时都是独立的)

   @Scheduled(fixedRate = 1000)