简体   繁体   中英

Spring Task Executor Scheduled too many instances of the task

I have a simple Spring Scheduled Taks defined by the following:

<context:component-scan base-package="com/test"/>
<task:scheduled-tasks>
    <task:scheduled ref="myScheduler" method="doMyTask" fixed-rate="300000"/>
</task:scheduled-tasks>

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

<task:executor id="executorWithPoolSizeRange"
               pool-size="1"
               queue-capacity="100"/>

<bean id="cleanupClass" class="com.test.CleanupClass">
   <property name="myProperty" value="3600"/>
</bean>

I would like to run a single thread synchronously every 5 minutes. However, what I get is FIVE instances of the task running consecutively every 5 minutes. Does anyone know if there is something missing from the XML description above?

I got the behavior I wanted using the @Scheduled annotations but I would rather not use annotation for the fixed-rate as I want it to be configurable outside of the code.

Thanks.

the following worked for me:

  <bean id="task" class="com.foo.MyTask">
  <task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="task" method="run" fixed-delay="300000" />
  </task:scheduled-tasks>
  <task:scheduler id="scheduler" pool-size="10" />

Greetings, Mark

Is this the behavior you are seeing in the STS when you deploy it to tomcat? If so, you would want to undeploy the application, redeploy it and restart the application.

Another idea is to use SPEL expression from a properties file to use it with @Sched annotation. In that way it is still configurable while using that annotation.

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