Is there a spring batch "job" scope?
Asked Answered
O

3

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.

Orpiment answered 10/9, 2012 at 14:15 Comment(2)
can you extend your question with more details about what you want to achieve?Calla
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.Orpiment
O
5

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

Orpiment answered 7/8, 2014 at 15:44 Comment(0)
S
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://mcmap.net/q/513991/-storing-in-jobexecutioncontext-from-tasklet-and-accessing-in-another-tasklet

Snobbery answered 12/11, 2012 at 10:54 Comment(1)
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 dataLaws
O
5

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

Orpiment answered 7/8, 2014 at 15:44 Comment(0)
C
0

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

Calla answered 10/9, 2012 at 21:9 Comment(3)
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.Orpiment
i am still not really sure about your usecase, but a simple threadsafe shared state could be accomplished with docs.oracle.com/javase/tutorial/essential/concurrency/…Calla
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.Orpiment

© 2022 - 2024 — McMap. All rights reserved.