我有以下任务计划程序设置:
<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 秒运行一次,无论之前的执行是否完成?
在您的 run 方法中放置 @Async 注释并查看
@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 -->
线程管理器.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 );
}
});
}
工作线程.java
@Component
public class WorkerThread {
public void execute(final Object request) {
// do the job
}
}
您可以根据您的要求进行自定义
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)