Storing in JobExecutionContext from tasklet and accessing in another tasklet

前端 未结 2 1216
梦毁少年i
梦毁少年i 2020-12-01 01:45

I have a requirement in which a tasklet, stores all the files in the directories in an arraylist. The size of the list is stored in the job execution context. Later this cou

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 02:02

    Another way is to use StepExecutionListener which is called after step execution. Your tasklet can implements it and share local attribute.

    public class ReadingJobExecutionContextTasklet implements Tasklet, StepExecutionListener {
        private String value;
    
        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            ExecutionContext jobExecutionContext = stepExecution.getJobExecution().getExecutionContext();
    
            jobExecutionContext.put("key", value);
            //Return null to leave the old value unchanged.
            return null;
        }
    }
    

    So, in the step, your bean is a tasklet and a listener like bellow. You should also configure the scope of you step to "step" :

        
            
                
                
                    
                
            
        
    
        
    

提交回复
热议问题