How to get job id using spring expression language?

后端 未结 4 1848
梦如初夏
梦如初夏 2021-01-13 10:42

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

4条回答
  •  悲&欢浪女
    2021-01-13 11:08

    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.

提交回复
热议问题