Closed
Description
Bug description
I have a Job
that contains a Step
that is annotated with @JobScope
(to be able to inject job parameters).
Using JobOperator.stop()
from another thread to stop that job fails with the following exception:
org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.step': Scope 'job' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for job scope
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:383) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:195) ~[spring-aop-5.3.6.jar:5.3.6]
at com.sun.proxy.$Proxy48.getName(Unknown Source) ~[na:na]
at org.springframework.batch.core.job.SimpleJob.getStep(SimpleJob.java:109) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.launch.support.SimpleJobOperator.stop(SimpleJobOperator.java:402) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.launch.support.SimpleJobOperator$$FastClassBySpringCGLIB$$44ee6049.invoke(<generated>) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.6.jar:5.3.6]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.6.jar:5.3.6]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.6.jar:5.3.6]
at org.springframework.batch.core.launch.support.SimpleJobOperator$$EnhancerBySpringCGLIB$$2759e0b7.stop(<generated>) ~[spring-batch-core-4.3.2.jar:4.3.2]
at com.example.demo.DemoApplication.lambda$runner$0(DemoApplication.java:54) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:819) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:803) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:346) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) ~[spring-boot-2.4.5.jar:2.4.5]
at com.example.demo.DemoApplication.main(DemoApplication.java:91) ~[classes/:na]
Caused by: java.lang.IllegalStateException: No context holder available for job scope
at org.springframework.batch.core.scope.JobScope.getContext(JobScope.java:159) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.scope.JobScope.get(JobScope.java:92) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371) ~[spring-beans-5.3.6.jar:5.3.6]
... 25 common frames omitted
Environment
Spring Batch: 4.3.2
Steps to reproduce
- Configure job with job-scoped steps.
- Run job asynchronously (to get hold of the pending
JobExecution
). - Use
JobOperator.stop()
to stop that job.
Expected behavior
Stopping the job works, regardless of the scope of its steps.
Minimal Complete Reproducible example
Sample project:
demo.zip
The project contains a job-scoped step. A CommandLineRunner
creates a JobLauncher
that launches jobs asynchronously.
Then, the main thread waits two seconds before it calls JobOperator.stop()
.
- Unzip.
- Run
./mvnw spring-boot:run
- Two seconds after the job was started,
JobOperator.stop()
is called and fails with the exception above.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
fmbenhassine commentedon Jun 14, 2023
Thank you for opening this issue and for providing a minimal example.
The problem here is the scoped step. A step should not be scoped (with step scope or job scope). what should be scoped instead is the component of the step (tasklet, reader, writer, etc). In your case, it is the item processor that should be scoped to use late-binding, not the step itself. Here is an example of how your sample should be changed:
With those changes, the sample works as expected.
There is a mention about not scoping
Step
beans in the documentation here, but it only mentions step-scope while it should include job scope as well.I will change this into a documentation issue and update the docs accordingly.
[-]JobOperator.stop() fails if Job contains @JobScoped steps[/-][+]Missing note about not scoping Step beans with Job scope[/+]marbon87 commentedon Jan 29, 2024
Hi @fmbenhassine ,
if steps should have scope step or job, how can i dynamically set the chunkSize from a job parameter?
In https://stackoverflow.com/a/67365622/4073333 your advice was to use @JobScope for the step, but that is not righy anymore, is it?
edit:
Is this a good solution?
fmbenhassine commentedon Dec 18, 2024
Done: 7481cf8
@marbon87 I don't know how I missed your message, apologies. Indeed, I talked about job-scoped steps in that answer on stackoverflow, but it seems like I should have elaborated more on the available options and, more importantly what is recommended and what is not. I updated that answer with more details: https://stackoverflow.com/a/67365622/5019386
Yes, just like the item reader or writer, the completion policy is the component to scope instead of the step itself. The docs were updated accordingly. I mentioned the fact that a step should not be scoped on SO, but I will iterate it here: while technically possible, a step should not be scoped. The reason is simple: scoping a step does not make sense, in fact it means: do not create that step definition until this step is started at runtime, but to start that step at runtime it needs to be defined upfront.. While one might think that the the step-scoped is for everything except steps, which should be job-scoped, the job scope was actually introduced for the sole reason to support JSR 352, which is was removed in v5. This is really a discussion for another thread, but if someone is using the job scope, then she/he is doing something wrong.
Add note about not scoping step beans with job scope
Add note about not scoping step beans with job scope
Add note about not scoping step beans with job scope