简体   繁体   中英

Spring batch : JobExecution from PreparedStatementSetter

I have a Spring batch with a PreparedStatementSetter for a query reader step. I would like to access to global data from the PreparedStatementSetter that is saved before into the ExecutionContext.

How could I access to the ExecutionContext from this PreparedStatementSetter ?

@Component
public class CurrentBatchIdPreparedStatementSetter implements PreparedStatementSetter
{
    @Override
    public void setValues(PreparedStatement preparedStatement) throws SQLException
    {
        // how to access to ExecutionContext here ?
    }
}

Thanks

YesWeCan

We can get ExecutionContext from step execution if the scope is step.

    @Component
    public class CurrentBatchIdPreparedStatementSetter implements PreparedStatementSetter
    {
       @Value("#{stepExecution}")
       private StepExecution stepExecution;

        @Override
        public void setValues(PreparedStatement preparedStatement) throws SQLException
        {
            final ExecutionContext executionContext = stepExecution.getExecutionContext();
            //Do your operations here
        }
    }

You can also get the same from job executioncontext.

  @Value("#{jobExecution}")
   private JobExecution jobExecution;
   //inside method
   final ExecutionContext executionContext = jobExecution.getExecutionContext();

or you can easily get value from job execution context.

        @Value ( "# {jobExecutionContext ['param1']}" ) 
        private String  param1;
        @Value ( "# {jobExecutionContext ['param2']}" )  
        private String  param2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM