是否可以在运行时获取Spring Batch中已定义作业的列表而不使用db?也许有可能从Serial
bean或类似的对象获取这些元数据?
答案 0 :(得分:5)
可以使用JobExplorer.getJobNames()
检索所有作业名称列表。
首先必须使用JobExplorerFactoryBean
定义jobExplorer
bean:
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
然后你可以在需要时注入这个bean。
答案 1 :(得分:1)
获取配置为bean的作业名列表的替代策略可以使用ListableJobLocator。
@Autowired
ListableJobLocator jobLocator;
....
jobLocator.getJobNames();
这不需要作业存储库。
答案 2 :(得分:1)
我使用这些代码列出并执行作业
private String jobName = "";
private JobLauncher jobLauncher = null;
private String selectedJob;
private String statusJob = "Exit Status : ";
private Job job;
ApplicationContext context;
private String[] lstJobs;
/**
* Execute
*/
public ExecuteJobBean() {
this.context = ApplicationContextProvider.getApplicationContext();
this.lstJobs = context.getBeanNamesForType(Job.class);
if (jobLauncher == null)
jobLauncher = (JobLauncher) context.getBean("jobLauncher");
}
/**
* Execute
*/
public void executeJob() {
setJob((Job) context.getBean(this.selectedJob));
try {
statusJob = "Exit Status : ";
JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
JobExecution execution = jobLauncher.run(getJob(), jobParameters);
this.statusJob = execution.getStatus() + ", ";
} catch (Exception e) {
e.printStackTrace();
this.statusJob = "Error, " + e.getMessage();
}
this.statusJob += " Done!!";
}
答案 3 :(得分:0)
要列出定义为bean的作业,只需让spring上下文为您将Job
类型的所有bean类型的作业注入到列表中,如下所示:
@Autowired
private List<? extends Job> jobs;
..
//You can then launch you job given a name.