Tomer
Tomer

Reputation: 31

Monitor the progress of a spring batch job

I am writing various jobs using spring batch with java configuration.

I need to get the current state of the job e.g. which steps are currently running (I may have multiple steps running at the same time) Which steps failed (the status and exit code) etc.

The only examples I see online are of XML based spring batch and I want to use java config only.

Thanks.

Upvotes: 2

Views: 7287

Answers (2)

Manuel Spigolon
Manuel Spigolon

Reputation: 12870

Another option is to use JobExplorer

Entry point for browsing executions of running or historical jobs and steps. Since the data may be re-hydrated from persistent storage, it may not contain volatile fields that would have been present when the execution was active.

List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
for (JobExecution jobExecution : jobExecutions) {
    jobExecution.getStepExecutions();
    //read step info
 }

And for create jobExplorer you have to use the factory:

import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
factory.setDataSource(dataSource);
factory.getObject();

Upvotes: 2

Sabir Khan
Sabir Khan

Reputation: 10132

I use these two queries on spring batch meta data tables to know about job progress and step details.

SELECT * FROM BATCH_JOB_EXECUTION ORDER BY START_TIME DESC;

SELECT * FROM BATCH_STEP_EXECUTION WHERE JOB_EXECUTION_ID=? ORDER BY STATUS;

With First query, I first find JOB_EXECUTION_ID corresponding to my job execution then use that id in second query to find details about specific steps.

Additionally, your config choice ( Java or XML ) has nothing to do with Spring Batch meta data. If you are persisting data then it doesn't matter if its XML config or Java Config.

For Java based monitoring- you can use JobExplorer & JobRepository beans to query jobs etc.

e.g. List<JobInstance> from jobExplorer.getJobInstances & jobExplorer.getJobExecutions(jobInstance) etc.

From JobExecutions you can get StepExecutions and so on.

You might have to set JobRegistryBeanPostProcessor bean like below for JobExplorer & JobRepository to work properly.

@Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(
        JobRegistry jobRegistry) {
    JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
    jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
    return jobRegistryBeanPostProcessor;
    }

Upvotes: 0

Related Questions