gpt4 book ai didi

java - 为什么 spring task scheduler 等待上一个任务完成?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:57 25 4
gpt4 key购买 nike

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

<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>

任务只打印一行并 hibernate 10 秒。使用此设置,我的期望是任务将每 5 秒运行一次,而不管之前的任务是否已完成执行(即停止 hibernate )。但事实并非如此,该任务每 15 秒运行一次( sleep 时间,然后在下一次 cron 被命中时)。

我如何配置它,使任务每 5 秒运行一次,而不管上一次执行是否完成?

最佳答案

在运行方法中放置@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>

然后是你的类(class)

 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 );
}
});
}

工作线程.java

@Component
public class WorkerThread {




public void execute(final Object request) {

// do the job
}

}

你可以根据自己的需要定制

关于java - 为什么 spring task scheduler 等待上一个任务完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13197904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com