Spring Package: getting a list of tasks at run time

Is it possible to get a list of tasks in Spring Batch at runtime without using db? Maybe it is possible to get this metadata from a jobRepository bean or some similar object?

+7
java spring spring-batch
source share
3 answers

You can get a list of all job names using JobExplorer.getJobNames() .

First you need to define a jobExplorer bean using JobExplorerFactoryBean :

 <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> 

and then you can enter this bean when you need it.

+5
source share

An alternative strategy for listing the names of jobs configured as beans is ListableJobLocator.

 @Autowired ListableJobLocator jobLocator; .... jobLocator.getJobNames(); 

This does not require a job repository.

+1
source share

I use this code to list and complete tasks

 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!!"; } 
0
source share

All Articles