我有一个Spring Batch应用程序,我想在其中安排作业调用。
在构建时不知道调度间隔,所以我不能用@ Scheduled注释我的Job。这导致我使用ThreadPoolTaskScheduler。
方法是将Runnable作为参数。是否有可能以这种方式安排工作? 我可以直接从以下服务调用该作业,但我无法安排它。
以下是我的问题背景,我试图简化:
@Service
public class ScheduledProcessor{
private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
private Application application;
@Autowired
public ScheduledProcessor(ThreadPoolTaskScheduler threadPoolTaskScheduler, Application application){
this.threadPoolTaskScheduler = threadPoolTaskScheduler;
this.application = application;
scheduledTasks = new ArrayList();
Trigger trigger = new CronTrigger("0/6 * * * * *");
//Here I am trying to schedule my job.
//The following line is wrong because a Job can't be cast to a Runnable but I wanted to show the intended behaviour.
threadPoolTaskScheduler.schedule((Runnable) application.importUserjob, trigger);
System.out.println("Job launch !");
}
这是JobBuilderFactory:
@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1, Step s2) {
return jobs.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(s1)
.end()
.build();
}
我理解(好吧,我甚至不确定)我不能直接将一个Job转换为Runnable但是可以以任何方式转换它吗?或者,您能否就能够动态安排弹簧批量作业的内容给出一些建议?
如果发生了变化,我还需要能够重启/跳过我的步骤,就像我目前可以使用threadPoolTaskScheduler一样。
提前感谢您提供的任何帮助或提示。
答案 0 :(得分:3)
我终于得到了怎么做!
我创建了一个实现Runnable的类(为方便起见,扩展了Thread,这样就不需要实现所有Runnable类了。)
@Component
public class MyRunnableJob extends Thread implements Runnable{
private Job job;
private JobParameters jobParameters;
private final JobOperator jobOperator;
@Autowired
public MyRunnableJob(JobOperator jobOperator) {
this.jobOperator = jobOperator;
}
public void setJob(Job job){
this.job=job;
}
@Override
public void run(){
try {
String dateParam = new Date().toString();
this.jobParameters = new JobParametersBuilder().addString("date", dateParam).toJobParameters();
System.out.println("jobName : "+job.getName()+" at "+dateParam);
jobOperator.start(job.getName(), jobParameters.toString());
} catch (NoSuchJobException | JobInstanceAlreadyExistsException | JobParametersInvalidException ex) {
Logger.getLogger(MyRunnableJob.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
在我的ScheduledProcessor类中,我将Job设置为myRunnable类,然后将其作为schedule方法的参数传递。
public class SchedulingProcessor {
//Autowired fields :
private final JobLauncher jobLauncher;
private final Job importUserJob;
private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
private final MyRunnableJob myRunnableJob;
//Other fields :
private List<ScheduledFuture> scheduledTasks;
@Autowired
public SchedulingProcessor(JobLauncher jobLauncher, Job importUserJob, ThreadPoolTaskScheduler threadPoolTaskScheduler, MyRunnableJob myRunnableJob) throws Exception {
this.jobLauncher=jobLauncher;
this.importUserJob=importUserJob;
this.threadPoolTaskScheduler=threadPoolTaskScheduler;
this.myRunnableJob=myRunnableJob;
Trigger trigger = new CronTrigger("0/6 * * * * *");
myRunnableJob.setJob(this.importUserJob);
scheduledTasks = new ArrayList();
scheduledTasks.add(this.threadPoolTaskScheduler.schedule((Runnable) myRunnableJob, trigger));
}
}
scheduledTasks列表只是为了控制我刚安排的任务。
这个技巧使我能够动态地(感谢ThreadPoolTaskScheduler)调度封装在实现Runnable的类中的Spring Batch Jobs。我希望它可以像我一样帮助某人。
答案 1 :(得分:0)
这是另一种从弹簧环境中触发它们的方式。
Job emailJob = (Job) applicationContext.getBean("xyzJob");
JobLauncher launcher = (JobLauncher) applicationContext
.getBean("jobLauncher");
launcher.run(emailJob, new JobParameters());