Unable to use Job Scope beans in a multi-threaded or partitioned step [BATCH-2269]

See original GitHub issue

David Geary opened BATCH-2269 and commented

Attempting to access a JobScope bean from a worker thread in a thread pool as part of a multi-threaded or partitioned step gives the following exception

Caused by: java.lang.IllegalStateException: No context holder available for job scope
        at org.springframework.batch.core.scope.JobScope.getContext(JobScope.java:153)
        at org.springframework.batch.core.scope.JobScope.get(JobScope.java:92)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:338)

This is due to the fact the JobExecution is stored in a ThreadLocal and it isn’t set up on the worker threads.


Affects: 3.0.1

Reference URL: http://stackoverflow.com/questions/24558703/multi-threaded-acces-to-job-scope-beans-in-spring-batch-3-0

18 votes, 21 watchers

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
spring-projects-issuescommented, Dec 16, 2019

Carlos Silva commented

We also had this problem, so we define a tweaked TaskExecutor that registers each thread in the JobSynchronizationManager, and in finally it releases it:

    TaskExecutor taskExecutor() {
        return new SimpleAsyncTaskExecutor() {
            @Override
            protected void doExecute(Runnable task) {
                //gets the jobExecution of the configuration thread
                def jobExecution = JobSynchronizationManager.context.jobExecution
                super.doExecute(new Runnable() {
                    @Override
                    void run() {
                        JobSynchronizationManager.register(jobExecution)

                        try {
                            task.run()
                        } finally {
                            JobSynchronizationManager.release()
                        }
                    }
                })
            }
        }
    }

Then we use that executor:

new FlowBuilder<SimpleFlow>('readControlFilesFlow')
               .start(firstStep)
               .split(taskExecutor())
               .add(anotherStep)
               .end()
0reactions
javanotescommented, Sep 25, 2020

If this is not fixed, can we please have the workaround updated in this issue itself? I am facing similar issues. This is the first time we are using Spring batch on our platform, and we will be using multi-threaded steps.

We need a context object for passing some data across steps. The ExecutionContext will not suffice in multithreaded access, as we will require a ‘read-then-update’ pattern (repeatable read isolation reqd). Hence we need our own context with a JobScope - and that is not working for the same reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-threaded acces to Job Scope beans in Spring Batch 3.0
In Spring Batch 3.0 I'm trying to use the new Job Scope functionality for beans in both partitioned and multi-threaded steps (configured ...
Read more >
Job Scoped Beans in Spring Batch - Medium
This job scope makes our life easier by ensuring we have a new instance every time a job is invoked and the same...
Read more >
Interview QA | Spring Batch Partitioning example | JavaTechie
This tutorial will give you complete picture about How to use spring batch Partitioning to process batch job faster with better performance ...
Read more >
Spring Batch Parallel Processing and Scaling 101 - Hevo Data
Multi-Threaded Step ; Parallel Steps; Remote Chunking; Partitioning. Conclusion ... You can use threads to run the partitions in parallel, ...
Read more >
Spring Batch partitioned step not running - Anycodings.com
Spring Batch partitioned step not running I was trying to configure my first anycodings_spring-batch multi-threaded ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found