简体   繁体   中英

No need to declare an extra TaskScheduler

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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