Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get job id using spring expression language?

Tags:

spring-batch

I want to get job id using spring expression language. I tried #{jobExecutionContext[jobId]} but it does not work.

like image 742
Mariusz Avatar asked Jun 17 '13 13:06

Mariusz


2 Answers

Using SpEL alone, there is no way to access the job id. You could use a JobExecutionListener to add it to the executionContext and then it would be available via what you are trying.

like image 157
Michael Minella Avatar answered Sep 21 '22 13:09

Michael Minella


A worked example would look like this. Your JobExecutionListener class has access to the JobExecution and it copies the jobId to the executionContext.

public class JobIdToContextExecutionListener implements JobExecutionListener {

    public void beforeJob(JobExecution jobExecution) {
        long jobId = jobExecution.getJobId();
        jobExecution.getExecutionContext().put("jobId",jobId);
    }

    ..
}

In your spring context, you can then reference the jobId via SpEL like

#{stepExecution.jobExecution.jobId}

or

#{jobExecutionContext.jobId}

See Luca's answer on referencing late-binding parameters here.

like image 39
emeraldjava Avatar answered Sep 21 '22 13:09

emeraldjava