0

Given that there is a Spring batch table that captures all the job execution params by key value:

create table BATCH_JOB_EXECUTION_PARAMS
(
    JOB_EXECUTION_ID BIGINT not null,
    TYPE_CD VARCHAR(6) not null,
    KEY_NAME VARCHAR(100) not null,
    STRING_VAL VARCHAR(250),
    DATE_VAL TIMESTAMP(26,6) default NULL,
    LONG_VAL BIGINT,
    DOUBLE_VAL DOUBLE,
    IDENTIFYING CHAR(1) not null,
    constraint JOB_EXEC_PARAMS_FK
        foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION
);

For example:

KEY_NAME correlationId
STRING_VAL eaa81b53-7f7d-4637-9935-b765405756be

I can query to retrieve the JOB_EXECUTION_ID

SELECT JOB_EXECUTION_ID FROM BATCH_JOB_EXECUTION_PARAMS
WHERE KEY_NAME = 'correlationId' AND STRING_VAL = 'eaa81b53-7f7d-4637-9935-b765405756be'

Then I can query the BATCH_JOB_EXECUTION table for full details of the job execution.

I have a rest endpoint where client provided this correlationId from which I need to grab the results of the job execution that includes BatchStatus, ExitStatus etc to build my response object.

My question is, how can I query to grab the JobExecution object from this param value provided? Is there anyway I can do this using JobExplorer?

1
  • FYI, the Spring Batch meta data tables seem to have changed over the years. See here. Commented Apr 17, 2023 at 21:35

2 Answers 2

1

Use the org.springframework.batch.core.explore.JobExplorer to search for job executions. If you have the executionId you can use getJobExecution.

This is a bean defined by SpringBatch so you can simply auto wire it in somewhere @Autowired JobExplorer jobExplorer.

There are also methods such as findRunningJobExecutions to find running jobs.

Sign up to request clarification or add additional context in comments.

2 Comments

I’m aware of this. But I need to lookup execution from one of the ids on the job params. I couldn’t locate any dao methods for job params. Or do I need to write my own queries
You probably will need to write your own query, but the easiest will be to save the executionId in another table where you have a row like : myCustomParam : executionId (assuming one-to-one), that will give you a very quick query.
0

You can do that using JobRepository#getLastJobExecution(String jobName, JobParameters parameters):

Map<String, JobParameter> parameters = new HashMap<>();
parameters.put("correlationId", new JobParameter("eaa81b53-7f7d-4637-9935-b765405756be", true));
JobParameters jobParameters = new JobParameters(parameters);
JobExecution jobExecution = jobRepository.getLastJobExecution("myJob", jobParameters);

5 Comments

So will provide the job execution that were completed from the batch_job_execution table? So persisted data from dB about historical job executions ?
1) yes, you will get the job execution for that parameter from the batch_job_execution table. 2) I don't understand this question. Can you elaborate?
thanks, it answers my question. Although it needs exact params used to call the job to generate the same job_key or else it trips up and does not return job instance. Can it also be done using JPA? i.e. public interface JobExecutionRepository extends JpaRepository<JobExecution, Long> { and writing a native query ?
Yes, you need all to add all identifying parameters so that Spring Batch can identify the job instance you are looking for and return the last execution. If you want to create a custom native query, then yes it's possible as well.
I'd have to have derive a new JobExecution entity in that case? I'd get Not a managed type: class org.springframework.batch.core.JobExecution if I use Spring JPA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.