1

I need to pass more that one variable between a Step Listener and a Writer. using StepCtx.setTransientUserData() i can only pass 1 variable.

So to pass multiple variables should i create a map/class and pass it or is there a better way?

Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
Fazil Hussain
  • 425
  • 3
  • 16

2 Answers2

2

setTransientUserData(Object) should be sufficient, the Object is constructed as you desire. You can create a more complex Object to hold many other Objects.

1

As you say, no matter the JobContext or StepContext, their method setTransientUserData(Object) can only pass 1 variable and this is not practical. Maybe they're just not designed for complex usage, I'm not sure.

However, you can define your own context to store variables. Make it singleton if you want to share this instance for every class :

import javax.inject.Named;
import javax.inject.Singleton;

@Named
@Singleton
public class MyContext {

    private int rowCount;
    private Map<String, String> params;
    // or other things ...

}

Then, you can use your customized context class MyContext in your ItemWriter, StepListener via CDI:

public class MyItemWriter implements ItemWriter {

    @Inject
    private MyContext myContext;

    // then use it in your preferred method
}

However, as you've mentioned step partitions in your previous question, your job is probably running in multi-thread. You should be aware of concurrency problem: you need to use the right data-structure for attributes in MyContext.

Mincong Huang
  • 5,284
  • 8
  • 39
  • 62