Spring 批处理:在运行时获取已定义作业的列表
Spring Batch: get list of defined jobs at runtime
是否可以在不使用 db 的情况下在运行时获取 Spring 批处理中已定义作业的列表?也许可以从 jobRepository
bean 或一些类似的对象中获取此元数据?
可以使用 JobExplorer.getJobNames()
检索所有作业名称的列表。
您首先必须使用 JobExplorerFactoryBean
:
定义 jobExplorer
bean
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
然后你可以在需要的时候注入这个bean。
获取配置为 bean 的作业名称列表的替代策略可以使用 ListableJobLocator。
@Autowired
ListableJobLocator jobLocator;
....
jobLocator.getJobNames();
这不需要作业存储库。
我使用这些代码来列出和执行作业
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!!";
}
要列出定义为 beans 的作业,您可以让 spring 上下文为您将类型为 Job
的所有 bean 类型注入到如下列表中:
@Autowired
private List<? extends Job> jobs;
..
//You can then launch you job given a name.
是否可以在不使用 db 的情况下在运行时获取 Spring 批处理中已定义作业的列表?也许可以从 jobRepository
bean 或一些类似的对象中获取此元数据?
可以使用 JobExplorer.getJobNames()
检索所有作业名称的列表。
您首先必须使用 JobExplorerFactoryBean
:
jobExplorer
bean
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
然后你可以在需要的时候注入这个bean。
获取配置为 bean 的作业名称列表的替代策略可以使用 ListableJobLocator。
@Autowired
ListableJobLocator jobLocator;
....
jobLocator.getJobNames();
这不需要作业存储库。
我使用这些代码来列出和执行作业
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!!";
}
要列出定义为 beans 的作业,您可以让 spring 上下文为您将类型为 Job
的所有 bean 类型注入到如下列表中:
@Autowired
private List<? extends Job> jobs;
..
//You can then launch you job given a name.