6

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

Mariusz
  • 1,907
  • 3
  • 24
  • 39

4 Answers4

4

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.

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
  • 3
    @MichaelMinella This answer is rather old, I think it's worth updating. Both `#{jobExecution.jobInstance.instanceId}` and `#{jobExecution.jobId}` provided the value of instance id for me. – jihor Jul 17 '17 at 15:39
4

Use scope="step" and then an expression in your query (or its parameters): #{stepExecution.jobExecution.id} (the root of the expression is a StepContext).

adramazany
  • 625
  • 9
  • 15
3

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.

Community
  • 1
  • 1
emeraldjava
  • 10,894
  • 26
  • 97
  • 170
1

#{stepExecution.jobExecution.id} or #{stepExecution.jobExecutionId} should work though.

The StepContext does provide access to the StepExecution for late binding via SpEL expressions.

Jimmy Praet
  • 2,230
  • 1
  • 15
  • 14