1

I have found that there is not need to declare an extra TaskScheduler and i can have my tasks like this:

<task:scheduled-tasks>
   <task:scheduled ref="runScheduler1" method="run" cron="*/5 * * * * *" />
   <task:scheduled ref="runScheduler2" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>

But could you help me with the explanation, why it is not needed like below?

<task:scheduled-tasks>
   <task:scheduled ref="runScheduler1" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>
<task:scheduled-tasks>
   <task:scheduled ref="runScheduler2" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>
bernardo
  • 23
  • 3

1 Answers1

0

Common Schedular

<task:scheduled-tasks>
   <task:scheduled ref="runScheduler1" method="run" cron="*/5 * * * * *" />
   <task:scheduled ref="runScheduler2" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>

This part defines one scheduler with two tasks in it. Both of the tasks will be executed independent of each other (as per their defined schedule). Having one scheuler with multiple tasks in it not only groups them together but also let you control a thread-pool common for both of the tasks.

    <task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="runScheduler1" method="run" fixed-rate="5000"  />
        <task:scheduled ref="runScheduler2" method="run" fixed-delay="500"  />
    </task:scheduled-tasks>

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

The above uses one scheduler and also tells that there are two tasks in my schedular with their own predefined fixed-delays. There is a possibility of both of the tasks and/or two occurances of a single task overlap eachother. In such cases those will be run concurrently under a thread pool of size 5.


Separate Scheduler

<task:scheduled-tasks>
   <task:scheduled ref="runScheduler1" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>
<task:scheduled-tasks>
   <task:scheduled ref="runScheduler2" method="run" cron="*/5 * * * * *" />
</task:scheduled-tasks>

However, in this example, there are two separate schedulers with one task in each. You can place different schedulers to different context xml files (if you have multiple context xmls). You can also have separate thread pools for each of them (like in above example).

As long as you dont want to have a logical separation between two tasks and you don't want to have separate thead-pools for each of them, the first approach should work for you.

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
  • The pool-size is based on the number of tasks within scheduled-task for example in above configuration pool-size = "2" ? – bernardo Mar 08 '18 at 03:24
  • Yes and No. In my example, there is one task which runs at a `fixed-rate` of 500ms and the method takes 2 seconds to complete. Which means when first task is running the next would start (after 500ms of the the start of first). Which means there can be 4 concurrent execution of the same task. – Amit Phaltankar Mar 08 '18 at 03:32
  • @bernardo As you have accepted my answer it looks like it answered your question. Can you please give my answer and upvote. Upvote not only help keep the answer on the top of the list but also help me earning some reputation. off-course, if you think I deserve an upvote :-) – Amit Phaltankar Mar 09 '18 at 00:58