1
votes

I try to run many scheduled-tasks in parallel with one instance every one I configure task like this

<task:scheduled ref="PatchData" method="start" fixed-rate="1000"/>

but there are many instances started every second while the first not finished. Is it possible to configure only one instance of the task run in same time ? My beans configuration in spring-scheduler.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.2.xsd">


    <bean id="UpdateScheduler" class="org.ws.scheduled.UpdateScheduler" />
    <bean id="PatchData" class="org.ws.scheduled.PatchData" />

    <task:scheduled-tasks scheduler="myScheduler">
           <task:scheduled ref="UpdateScheduler" method="start" cron="0 30 14 * * *"/>            
           <task:scheduled ref="PatchData" method="start" fixed-rate="1000"/>

    </task:scheduled-tasks>

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

1
Maybe what you want is fixed-delay instead of fixed-rate? More info at docs.spring.io/spring-framework/docs/current/… - Boris
If you have multiple instances started at the same time you are loading the context multiple times. If you want a single instance to run each time use fixed-delay instead of fixed-rate. - M. Deinum

1 Answers

1
votes

You can add profile setting in bean tag. Example :

<beans profile="scheduledProfile">
 <bean id="UpdateScheduler" class="org.ws.scheduled.UpdateScheduler" />
 <bean id="PatchData" class="org.ws.scheduled.PatchData" />

 <task:scheduled-tasks scheduler="myScheduler">
      <task:scheduled ref="UpdateScheduler" method="start" cron="0 30 14 * * *"/>            
      <task:scheduled ref="PatchData" method="start" fixed-rate="1000"/>
 </task:scheduled-tasks>
<bean />

And, start your application one machine with the scheduled profile activated = -Dspring.profiles.active=scheduledProfile

In this way, this Schedule works only one instance.