9

I just want to know if there is a "JOB" scope in spring batch, like the "STEP" scope ? If there is not, should we develop our custom scope, or is there a better alternative ?

Thanks in advance.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Oussama Zoghlami
  • 1,660
  • 17
  • 24
  • can you extend your question with more details about what you want to achieve? – Michael Pralow Sep 10 '12 at 20:08
  • I have created a dataHolder bean to share data between different steps of the Job. My job, could be executed asynchrounously by multiple threads, so for thread-safety requirements, i did want to create a dataHolder bean per each executed Job. – Oussama Zoghlami Sep 10 '12 at 20:57

3 Answers3

6

A jira has been opened some times ago on the spring batch issues tracker regarding this issue: https://jira.springsource.org/browse/BATCH-1701

A pull request has been submitted as well, hopefully it will be merge soon, because I have this use case too: https://github.com/SpringSource/spring-batch/pull/41

There are multiple workarounds in the mean time, each with its drawback, see this answer on stackoverflow: https://stackoverflow.com/a/8121102/470107

Community
  • 1
  • 1
Raphaël Brugier
  • 480
  • 5
  • 14
  • I think the third link the correct way to do it: you have a "Job execution scope", which is better than a "job" scope if you want to have each thread isolated with its own data – Pablo Lozano Aug 01 '14 at 12:50
5

Finally Spring Batch 3.0 supports now the Job Scope : What's new in Spring Batch 3.0

Oussama Zoghlami
  • 1,660
  • 17
  • 24
0

if your dataHolder bean holds state you could try it with a proper bean scope e.g. prototype

Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
  • But with the prototype scope, each time i will reference my dataHolder bean from a job step, a new bean would be created ? Me, i would like to use the same dataHolder bean from all a given job's steps. – Oussama Zoghlami Sep 10 '12 at 21:30
  • i am still not really sure about your usecase, but a simple threadsafe shared state could be accomplished with http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html – Michael Pralow Sep 10 '12 at 22:12
  • Thank you michael for your help, to resolve my problem, i finally create a dataHolderResolver Bean (Singleton), containing the method getDataHolder(int jobId) returning the DataHolder associated to the given jobId. I declared my dataHolder bean as a prototype, and i used the lookup-method spring feature on the dataHolderResolver bean, to get a new dataHolder reference, if it's not yet created and cached for a given jobId. But, i think that it will be usefull to have the 'job' scope in next version of Spring batch. – Oussama Zoghlami Sep 11 '12 at 21:37