繁体   English   中英

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

[英]Why does spring task scheduler wait for previous task to finish?

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

<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秒运行一次,无论先前的执行是否完成?

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

}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM