javajakarta-eejava-batchjberet

Java-Batch: How to pass data from one step to another step?


in my JBeret java-batch job I need to pass parameters from one step to the next step. So far I have only figured out to do that via JobContext.setTransientUserData().

My questions are: Is the transientUserData way the best-practice way or are there better alternatives? Is it common to pass parameters between batch steps or are steps supposed to be self-sufficient in this regard?


Solution

  • Yes, job context transient user data is the standard mechanism for passing application data between steps, as defined by the batch spec (JSR 352). This usage is portable and works in all compliant implementations.

    A step is self-contained for the most part, but steps also live inside a job. So it is common for a step to export or consume application data from another step, to coordinate job execution.

    In JBeret, you can declare CDI beans as @JobScoped, and inject it into where it is needed to access the shared application state.

    See this JBeret test for example usage.

    A similar stackoverflow discussion: How to put in custom scope/context (JobScoped - custom CDI scope) particular instance from request to make it injectable?