Java – why does the spring task scheduler wait for the last task to complete?

I have the following task scheduler settings:

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

The task prints only one line and sleeps for 10 seconds With this setting, my expectation is that the task will run every 5 seconds, regardless of whether the previous task has completed execution (i.e. stop hibernation) But this is not the case. The task runs for 15 seconds at a time (sleep time, and then the next cron hit)

How do I configure this task so that it runs every 5 seconds, regardless of whether the previous execution is complete or not?

Solution

Put @ async anonation in your running method and view it

@Async
   public void run{

   }

Or you can

Try this

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

What about your class

package foo.bar;

 public class Task{

  public void fooMethod(){
  // do task
 }

}

Add on request

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

}

You can customize it according to your requirements

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>