How To Propagate Context Information Throw Spring Batch

Introduction

While developing applications using Spring batch, we sometimes face one or most of the following cases:

  • The necessity of getting the security context to call methods or perform processing that requires security authorization.
  • Propagating the trace Id and Span Id required by Sleuth in micro-service context.
  • Getting the user Locale (i18n) in order to generate internationalized output.
  • Printing MDC information.

Those cases can be resolved by passing the context information as job parameters and restoring them before the job or step runs using JobExecutionListener or StepExecutionListener, respectively, according to the execution configuration made (One thread per job or thread pool that).