Gan
Gan

Reputation: 634

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 * * * ? 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 .

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.

Upvotes: 1

Views: 1568

Answers (3)

M. Deinum
M. Deinum

Reputation: 124516

By default the scheduler uses a ConcurrentTaskScheduler with a single thread. If you want another one configure it and pass it to the scheduled-tasks scheduler attribute.

The easiest way, in XML, is to use the scheduler element. (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"> ...

Upvotes: 2

Martin Hansen
Martin Hansen

Reputation: 2101

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.

Upvotes: 0

SkyWalker
SkyWalker

Reputation: 29150

Have you used @EnableScheduling in your java code?

@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

For more, you can go through

  1. Spring 3 @Scheduled – 4 Ways to Schedule Tasks
  2. Spring Batch + Spring TaskScheduler example
  3. Scheduling Tasks

Enable scheduling annotations

To enable support for @Scheduled and @Async annotations add @EnableScheduling and @EnableAsync to one of your @Configuration classes:

@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. For more fine-grained control you can additionally implement the SchedulingConfigurer and/or AsyncConfigurer interfaces. See the javadocs for full details.

If you prefer XML configuration use the <task:annotation-driven> element.

<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.

Upvotes: 0

Related Questions