我認爲你可以通過使用spring的「@Async」或者你可以創建自己的線程池來完成Job的Job.run方法改變爲Asynchronous方法。
/**
* Created by roman.luo on 2016/9/14.
*/
@Component
@Scope("prototype")
public class JobDelegate implements Job {
private Job job;
public JobDelegate(Job job) {
this.job = job;
}
@Async
public void run(){
job.run();
}
}
/**
* Created by roman.luo on 2016/9/14.
*/
@Component
public class Sceduled extends ApplicationObjectSupport{
@Scheduled(fixedRate = 100)
public void run(){
Job job = QUEUE.take();
Job jobDelegate = getApplicationContext().getBean(JobDelegate.class,job);
jobDelegate.run();
}
}
記得配置Spring的XML文件:
<task:executor id="myexecutor" pool-size="5" />
<task:annotation-driven executor="myexecutor"/>