简体   繁体   English

为什么Spring Task Scheduler无法同时执行任务?

[英]why spring task scheduler not executing task simultaneously?

I have following configuration to run task-- 我有以下配置来运行任务-

<bean id="trendDataJob" class="com.ge.og.realtrack.scheduler.TrendDataJob"> </bean>

<task:scheduled-tasks>
             <task:scheduled ref="trendDataJob" method="trendJob" cron="#{trendDataJob.configMap['corn_exp']}"></task:scheduled>
             <task:scheduled ref="trendDataJob" method="metaDataTrendJob" cron="#{trendDataJob.configMap['metadata_corn_exp']}"></task:scheduled>
</task:scheduled-tasks>  

cron expression for this is corn_exp=0 0/1 * * * ? 为此的cron表达式是corn_exp=0 0/1 * * * ? to run every minute. 每分钟运行一次。

Here is problem as both method of trendDataJob schedule to run every minute but they are executing one after another first trendJob once its completed then its executing metaDataTrendJob i am not able to understand this behavior . 这是一个问题,因为两种趋势数据作业时间表都必须每分钟运行一次,但是它们在一个第一个TrendJob完成之后又一个接一个地执行,然后执行的metaDataTrendJob我却无法理解这种行为。

Also another problem is in case of method takes more than one minute to finish finish..its not triggering next call till current call finish and return. 另外一个问题是方法要花费一分钟以上才能完成完成。在当前调用完成并返回之前,它不会触发下一个调用。

By default the scheduler uses a ConcurrentTaskScheduler with a single thread. 默认情况下,调度程序使用具有单个线程的ConcurrentTaskScheduler If you want another one configure it and pass it to the scheduled-tasks scheduler attribute. 如果要另一个配置它,并将其传递给scheduled-tasks scheduler属性。

The easiest way, in XML, is to use the scheduler element. 在XML中,最简单的方法是使用scheduler元素。 (See this section in the reference guide). (请参阅参考指南中的本节 )。

<task:scheduler id="scheduler" pool-size="10"/>

Then simply register it on the other element. 然后只需将其注册到另一个元素上即可。

<task:scheduled-tasks scheduler="scheduler"> ...

Have you used @EnableScheduling in your java code? 您是否在Java代码中使用了@EnableScheduling

@EnableScheduling ensures that a background task executor is created. @EnableScheduling确保创建了后台任务执行程序。 Without it, nothing gets scheduled. 没有它,什么都无法安排。

For more, you can go through 要了解更多,您可以通过

  1. Spring 3 @Scheduled – 4 Ways to Schedule Tasks Spring 3 @Scheduled –安排任务的4种方法
  2. Spring Batch + Spring TaskScheduler example Spring Batch + Spring TaskScheduler示例
  3. Scheduling Tasks 计划任务

Enable scheduling annotations 启用计划注释

To enable support for @Scheduled and @Async annotations add @EnableScheduling and @EnableAsync to one of your @Configuration classes: 要启用对@Scheduled@Async批注的支持,请将@EnableScheduling@EnableAsync添加到您的@Configuration类之一:

@Configuration
@EnableAsync
@EnableScheduling
public class AppConfig {
}

You are free to pick and choose the relevant annotations for your application. 您可以自由选择适合您的应用程序的相关注释。 For example, if you only need support for @Scheduled , simply omit @EnableAsync . 例如,如果只需要对@Scheduled支持,则只需省略@EnableAsync For more fine-grained control you can additionally implement the SchedulingConfigurer and/or AsyncConfigurer interfaces. 为了获得更细粒度的控制,您可以另外实现SchedulingConfigurer和/或AsyncConfigurer接口。 See the javadocs for full details. 有关完整的详细信息,请参见javadocs。

If you prefer XML configuration use the <task:annotation-driven> element. 如果您更喜欢XML配置,请使用<task:annotation-driven>元素。

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>

Notice with the above XML that an executor reference is provided for handling those tasks that correspond to methods with the @Async annotation, and the scheduler reference is provided for managing those methods annotated with @Scheduled. 请注意,使用上述XML时,提供了执行程序引用来处理与带有@Async批注的方法相对应的那些任务,并且提供了调度程序引用来管理以@Scheduled注释的那些方法。

If you're using a default task scheduler in spring, i'm pretty sure it only runs on a single thread, hence why you cannot make them run in parallel. 如果您在春季使用默认任务计划程序,则可以确定它只能在单个线程上运行,因此为什么不能使它们并行运行。

You need to configure some kind of BatchScheduler with a pool size, to make it run in parallel. 您需要配置某种具有池大小的BatchScheduler,以使其并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM