2012-11-02 46 views
6

我有以下的任务调度设置:为什么spring任务计划程序等待上一个任务完成?

<bean id="Task" class="foo.bar.Task" /> 

<bean id="TaskScheduler" 
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"> 
    <property name="waitForTasksToCompleteOnShutdown" value="true" /> 
    <property name="poolSize" value="1000" /> 
</bean> 

<task:scheduled-tasks scheduler="TaskScheduler"> 
    <task:scheduled ref="Task" method="run" cron="*/5 * * * * *" /> 
</task:scheduled-tasks> 

的任务只是打印一条线,休眠10秒。通过这种设置,我的期望是任务每5秒钟运行一次,而不管前一个任务是否完成了它的执行(即停止睡眠)。但事实并非如此,该任务一次运行15秒(睡眠时间,然后下一次cron命中)。

如何配置此选项,以便每5秒执行一次任务,而不管先前的执行是否完成?

回答

9

在您运行方法把@Async anotation看

@Async 
    public void run{ 

    } 

,或者你可以

尝试这个

<bean id="schedulerTask" 
     class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> 
    <property name="mytaskClass" ref="mytaskClass" /> 
    <property name="targetMethod" value="fooMethod" /> 
</bean> 

<bean id="mytaskClass" class="foo.bar.Task" /> 

<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
    <property name="timerTask" ref="schedulerTask" /> 
    <property name="delay" value="10" /> 
    <property name="period" value="5000" /> 
</bean> 

<bean class="org.springframework.scheduling.timer.TimerFactoryBean"> 
    <property name="scheduledTimerTasks"> 
     <list> 
      <ref local="timerTask" /> 
     </list> 
    </property> 
</bean> 

然后你的类

package foo.bar; 

public class Task{ 

    public void fooMethod(){ 
    // do task 
} 

} 

添加根据请求

<!-- Thread pool related configurations --> 
    <bean name="workerThread" class="foo.WorkerThread"/> 

    <bean name="managerThread" class="foo.ManagerThread" > 
    <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" /> 
    <constructor-arg type="foo.process.WorkerThread" ref="workerThread"/> 
    </bean> 

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > 
<property name="corePoolSize" value="5" /> 
<property name="maxPoolSize" value="30" /> 
<property name="queueCapacity" value="100" /> 
</bean> 
<!-- End Thread pool related configurations --> 

ManagerThread.java

public class ManagerThread { 

private TaskExecutor taskExecutor=null; 
private WorkerThread workerThread=null; 


/** 
* @param taskExecutor 
* @param workerThread 
*/ 
public ManagerThread(final TaskExecutor taskExecutor,final WorkerThread workerThread) { 

    this.taskExecutor = taskExecutor; 
    this.workerThread = workerThread; 
} 


/** 
* Create a new thread and execte the requests 
* @param parameter 
*/ 
public synchronized void fire(final Object parameter) { 
    taskExecutor.execute(new Runnable() { 
     public void run() { 
      workerThread.execute(parameter); 
     } 
    }); 
    } 

WorkerThread.java

@Component 
public class WorkerThread { 




public void execute(final Object request) { 

    // do the job 
    } 

} 

你可以自定义此按您的要求

+0

感谢您的回复。 _ @ Async_起作用。 TimerFactoryBean已被弃用,所以我不想使用它。通常我喜欢将Spring代码/注释保留在我的代码之外。有没有办法通过配置来实现_ @ Async_的等价物? – GuerillaNerd

+0

是的,我添加了一些配置和可能的java类。 – Suranga

0

您可以增加线程池的大小,以允许多个cron作业同时运行。

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