简体   繁体   中英

Query batch job metadata in Spring batch

I want to fetch the 10 latest records from the BATCH_JOB_EXECUTION-table joined with the BATCH_JOB_INSTANCE-table.

So how can I access these tables?

In this application I have used Spring Data JPA. It's another application which uses Spring Batch and created these tables. In other words, I would just like to run a JOIN-query and map it directly to my custom object with just the necessary fields. As far as it's possible, I would like to avoid making seperate models for the two tables. But I don't know the best approach here.

If you want to do it from Spring Batch code you need to use JobExplorer and apply filters on either START_TIME or END_TIME . Alternatively, just send an SQL query with your desired JOIN to the DB using JDBC. The DDLs of the metadata tables can be found here .

EDIT

If you want to try to do it in SpringBatch, I guess you need to iterate through JobExecutions and find the ones that interest you, then do your thing )) someth. like:

List<JobInstance> jobInstances = jobExplorer.getJobInstances(jobName);

for (JobInstance jobInstance : jobInstances) {
    List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
    for (JobExecution jobExecution : jobExecutions) {
        if (//jobExecution.getWhatever...)) {
        // do your thing...
        }
     }
} 

Good Luck!

Since JobExplorer doesn't have the interface .getJobInstances(jobName) anymore, I have done this (this example with BatchStatus as a condition) adapted with streams :

List<JobInstance> lastExecutedJobs = jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE);

Optional<JobExecution> jobExecution = lastExecutedJobs
                                .stream()
                                .map(jobExplorer()::getJobExecutions)
                                .flatMap(jes -> jes.stream())
                                .filter(je -> BatchStatus.COMPLETED.equals(je.getStatus()))
                                .findFirst();

To return N elements, you could use others capacities of stream (limit, max, collectors, ...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM