Java 为什么spring任务调度器会等待上一个任务完成?
我有以下任务计划程序设置:Java 为什么spring任务调度器会等待上一个任务完成?,java,spring,Java,Spring,我有以下任务计划程序设置: <bean id="Task" class="foo.bar.Task" /> <bean id="TaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"> <property name="waitForTasksToCompleteOnShutdown" value="true" /> &
<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秒运行一次,而不管上一次执行是否完成?在运行方法put@Async anotion时,请参见
@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>
根据要求添加
<!-- 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 -->
WorkerThread.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 );
}
});
}
@Component
public class WorkerThread {
public void execute(final Object request) {
// do the job
}
}
您可以根据自己的需求自定义此设置您可以增加线程池大小以允许多个cron作业同时运行
<task:scheduler id="taskScheduler" pool-size="10"/>
谢谢您的回复@异步工作。TimerFactoryBean已弃用,因此我不想使用它。通常,我喜欢将spring代码/注释排除在代码之外。有没有办法通过配置实现@Async的等效功能?是的,我添加了一些配置和可能的java类。
<task:scheduler id="taskScheduler" pool-size="10"/>