Shamik
Shamik

Reputation: 1731

Can we create a pool of Spring ScheduledExecutorTask?

I'm trying to see if there's a possibility to create a pool of spring ScheduledExecutor. What I need is a set a ScheduledExecutor tasks which will perform certain task in a regular interval. I was trying to use the following way :

<bean id="contentProcessorPool"
    class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
<property name="poolSize" value="${processor.corepoolsize}"/>
<property name="continueScheduledExecutionAfterException" value="true"/>
<property name="scheduledExecutorTasks">
  <list>
    <ref local="processor"/>
  </list>
</property>

<bean id="processor"
    class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<property name="delay" value="${processor.polling.delay}"/>
<property name="period" value="${processor.polling.period}"/>

<property name="runnable">
  <ref local="contentWorker" />
</property>

 <bean id="contentWorker" class="com.autodesk.contentextraction.processor.ContentWorker">
</bean>

But this creates one a single ContentWorker instance which keeps running at the specified interval. What I want is a set of ContentWorker to run after a given interval.

Any pointers will be highly appreciated.

Thanks

Upvotes: 2

Views: 2286

Answers (2)

Nick Robson
Nick Robson

Reputation: 71

If you used fixed rate instead of fixed delay the operation will occur exactly at that interval, and not after the following one has complete. I wouldn't have thought you'd need to pool it (manually) after that?

If you don't need explicit control in your context file, you can annotate the class:

import org.springframework.stereotype.Service
org.springframework.scheduling.annotation.Scheduled

Class annotation: @Service 
Method annotation: @Scheduled(fixedRate=30000)

Context:

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd

<task:annotation-driven />

Upvotes: 1

cheekoo
cheekoo

Reputation: 877

How about this?

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="someObject" method="someMethod" fixed-delay="5000"/>
</task:scheduled-tasks>

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

Source: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html#scheduling-task-namespace-scheduled-tasks

Upvotes: 3

Related Questions