3

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?

uwl
  • 33
  • 5

1 Answers1

5

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?

Community
  • 1
  • 1
cheng
  • 1,076
  • 6
  • 6
  • Thank you very much for this answer! I thought the same about batch steps and passing values via setTransientUserData(). I overlooked the @JobScoped annotation. This is the way I will do it. It seems cleaner and easier to have the bean injected than to pass it as transientUserData in the JobContext. And thank you for the link to the other discussion. I did not find that when I searched. – uwl Jan 23 '17 at 12:25
  • I read about transient/persistend user data and like this concept. Would it not also be feasible to pass information by simply storing it in the job or step context via Properties? – Queeg Oct 05 '20 at 06:32