2012-11-02 35 views
6

Tôi có thiết lập task scheduler sau:Tại sao bộ lập lịch tác vụ mùa xuân chờ nhiệm vụ trước đó kết thúc?

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

Tổ công tác chỉ in một dòng và ngủ trong vòng 10 giây. Với thiết lập này, kỳ vọng của tôi là nhiệm vụ sẽ chạy sau mỗi 5 giây, bất kể tác vụ trước đó đã hoàn thành nó là thực thi (tức là ngừng ngủ). Nhưng đó không phải là trường hợp, nhiệm vụ chạy một lần bao giờ 15 giây (thời gian ngủ và sau đó thời gian tiếp theo cron là hit).

Làm cách nào tôi có thể định cấu hình thao tác này để nhiệm vụ chạy sau mỗi 5 giây bất kể việc thực hiện trước đó đã kết thúc chưa?

Trả lời

9

Trong bạn chạy phương pháp đặt @Async anotation và xem

@Async 
    public void run{ 

    } 

hoặc bạn có thể

Hãy thử này

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

Sau đó, lớp học của bạn

package foo.bar; 

public class Task{ 

    public void fooMethod(){ 
    // do task 
} 

} 

gia tăng như theo yêu cầu

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

} 

Bạn có thể tùy chỉnh này theo yêu cầu của bạn

+0

Cảm ơn bạn đã trả lời. _ @ Async_ hoạt động. TimerFactoryBean không được chấp nhận, vì vậy tôi không muốn sử dụng nó. Theo quy tắc, tôi muốn giữ mã/chú giải vào mã của tôi. Có cách nào để làm tương đương với _ @ Async_ thông qua cấu hình? – GuerillaNerd

+0

Có, tôi đã thêm một số cấu hình và các lớp java có thể. – Suranga

0

Bạn có thể tăng kích thước chủ đề hồ bơi để cho phép nhiều công việc cron để chạy đồng thời.

<task:scheduler id="taskScheduler" pool-size="10"/> 
Các vấn đề liên quan