获取“作用域'步骤'对于当前线程是不活跃的".在创建春季批豆时 [英] Getting "Scope 'step' is not active for the current thread" while creating spring batch beans

查看:97
本文介绍了获取“作用域'步骤'对于当前线程是不活跃的".在创建春季批豆时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring批处理配置中,我试图设置一个分区步骤,该步骤从JobParameters访问值,如下所示:

In my Spring batch configuration, I'm trying to setup a partitioned step, which accesses values from JobParameters as follows :

@Bean
@Qualifier("partitionJob")
public Job partitionJob() throws Exception {

    return jobBuilderFactory
            .get("partitionJob")
            .incrementer(new RunIdIncrementer())
            .start(partitionStep(null))
            .build();
}

@Bean
@StepScope //I'm getting exception here - > Error creating bean 
public Step partitionStep(
        @Value("#{jobParameters[gridSize]}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(
                    StringUtils.isEmpty(gridSize) ? 10 : Integer
                            .parseInt(gridSize))
            .step(slaveStep(50000))
            .taskExecutor(threadPoolTaskExecutor()).build();
}

@Bean
@StepScope
public Step slaveStep(int chunkSize) throws Exception {

    return stepBuilderFactory
            .get("slaveStep")
            .<Person,Person> chunk(chunkSize)
            .reader(jdbcPagingItemReader()),
            .writer(csvFileWriterParts())
            .listener(stepExecutionListener()).build();
}

我已经在我的SpringBoot应用程序中添加了@EnableBatchProcessing批注.

I have added @EnableBatchProcessing annotation to my SpringBoot application.

由于我想在构建步骤时访问JobParameters,因此我使用了@StepScope.我有一个很好的示例,没有@StepScope注释,但是在那种情况下,我没有访问任何JobParameters或上下文中的任何内容.

Since I wanted to access JobParameters while constructing a step, I used @StepScope. I have an example that works fine, without @StepScope annotation, but in that case I'm not accessing any JobParameters or anything from context.

但是,如果我在partitionStep上使用StepScope批注,我会得到

But If I use StepScope annotation on partitionStep, I'm getting

创建名称为'scopedTarget.partitionStep'的bean时出错:作用域 'step'对当前线程无效;考虑定义一个 如果您打算从一个Bean引用此Bean的作用域代理, 单身人士嵌套异常是java.lang.IllegalStateException:否 上下文持有人可用于步骤范围

Error creating bean with name 'scopedTarget.partitionStep': Scope 'step' 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 step scope

但是如果我将其更改为JobScope,则它会在slaveStep()上失败,并显示相同的错误消息.

but If I change it to JobScope, then it is failing at slaveStep() with same error message.

在这种情况下使用的正确范围是什么?如何解决我遇到的这个问题?

What is the correct scope to be used in such cases and how to resolve this issue I'm getting ?

在配置spring bean时访问JobParameters的更好方法是什么?

What is the better way of accessing JobParameters while configuring spring beans ?

异常堆栈如下

2018-05-25 21:07:32,075错误[主要] org.springframework.batch.core.job.AbstractJob:遇到致命事故 执行作业时出错 org.springframework.beans.factory.BeanCreationException:错误 创建名称为"scopedTarget.partitionStep"的bean:作用域"step"为 对于当前线程无效;考虑定义作用域代理 如果您打算从单例中引用它,则为该Bean;嵌套的 异常是java.lang.IllegalStateException:没有上下文持有者 可用于以下步骤范围 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在 org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) 在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) 在com.sun.proxy.$ Proxy55.getName(未知来源)处 org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:115) 在 org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:392) 在 org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) 在 org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) 在 org.springframework.batch.core.launch.support.SimpleJobLauncher $ 1.run(SimpleJobLauncher.java:135) 在 org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) 在 org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 在 org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration $ PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 在com.sun.proxy.$ Proxy54.run(未知来源) com.sample.main(ExtractApplication.java:58) 原因:java.lang.IllegalStateException:没有上下文持有者 可用于以下步骤范围 org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:167) 在 org.springframework.batch.core.scope.StepScope.get(StepScope.java:99) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346) ...省略了23个共同的框架

2018-05-25 21:07:32,075 ERROR [main] org.springframework.batch.core.job.AbstractJob : Encountered fatal error executing job org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.partitionStep': Scope 'step' 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 step scope at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) at com.sun.proxy.$Proxy55.getName(Unknown Source) at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:115) at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:392) at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) at com.sun.proxy.$Proxy54.run(Unknown Source) at com.sample.main(ExtractApplication.java:58) Caused by: java.lang.IllegalStateException: No context holder available for step scope at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:167) at org.springframework.batch.core.scope.StepScope.get(StepScope.java:99) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346) ... 23 common frames omitted

如果我修改JobScope,则会在slaveStep上收到异常,这与上面的异常类似.

If I modify to JobScope, I get exception on slaveStep, which is similar to the above exception.

推荐答案

请尝试以下选项

Please try the option Spring batch scope issue while using spring boot, posted by Manh. I guess it solved the problem. Unfortunately I no longer have access to the code base, to confirm what I did for the fix.

这篇关于获取“作用域'步骤'对于当前线程是不活跃的".在创建春季批豆时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆