为什么spring任务调度程序等待上一个任务完成?

编程入门 行业动态 更新时间:2024-10-27 11:26:20
本文介绍了为什么spring任务调度程序等待上一个任务完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下任务调度程序设置:

I have the following task scheduler setup:

<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被击中)。

The Task just prints a line and sleeps for 10 seconds. With this setup, my expectation was that the task would run every 5 seconds, regardless of whether the previous task had finished it's execution (ie stopped sleeping). But that's not the case, the task runs once ever 15 seconds (the sleep time and then the next time the cron is hit).

我如何配置这个以便无论先前的执行是否完成,任务都会每5秒运行一次?

How can I configure this so that the task runs every 5 seconds regardless of whether the previous execution finished?

推荐答案

在运行方法中放置@Async anotation并查看

In you run method put @Async anotation and see

@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

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

WorkerThread.java

@Component public class WorkerThread { public void execute(final Object request) { // do the job } }

您可以根据您的要求自定义

You could customize this as per your requirement

更多推荐

为什么spring任务调度程序等待上一个任务完成?

本文发布于:2023-11-06 14:48:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!