web-dev-qa-db-ja.com

Spring Boot使用中のSpring Batch Scopeの問題

私はスタンドアロンの春のバッチジョブを持っています。 JUNITの場合、これは完全に正常に動作します

@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = KPBootApplication.class)
@ContextConfiguration(locations={"classpath:kp-sb.xml"})
public class KPBootApplicationTests {

    private final static Logger LOG=LoggerFactory.getLogger(KPBootApplicationTests.class);

    @Autowired
    ApplicationContext context;

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job job;

    @Test
    public void testJob() {

        final JobParameters jobParameters = new JobParametersBuilder()
                .toJobParameters();

        JobExecution execution;
        try {
            execution = jobLauncher.run(job, jobParameters);
            final ExitStatus status = execution.getExitStatus();

            if (ExitStatus.COMPLETED.getExitCode().equals(status.getExitCode())) {
                LOG.info("Job completed Yeaaaaaaaa!!!!!");
            } else {
                final List<Throwable> exceptions = execution
                        .getAllFailureExceptions();
                for (final Throwable throwable : exceptions) {
                    LOG.error(throwable.getMessage(), throwable);

                }
            }
        } catch (JobExecutionAlreadyRunningException e) {
            LOG.error(e.getMessage(), e);
        } catch (JobRestartException e) {
            LOG.error(e.getMessage(), e);
        } catch (JobInstanceAlreadyCompleteException e) {
            LOG.error(e.getMessage(), e);
        } catch (JobParametersInvalidException e) {
            LOG.error(e.getMessage(), e);
        }

    }

}

そして設定ファイル

<!-- Below code till Job Repo is commented out during spring-boot -->
<context:property-placeholder
    properties-ref="kpProps" />
<util:properties id="kpProps">
    <prop key="app.file.path">
        D:/temp/kp1/all
    </prop>
    <prop key="app.max_thread_num">
        10
    </prop>
</util:properties>

<!-- 
<bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

<bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="batchTransactionManager" />
</bean>
 -->

<bean id="batchTransactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
    <property name="rollbackOnCommitFailure" value="false" />
</bean>

<bean id="multiResourcePartitionerReq"
    class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
    <property name="resources" value="file:${app.file.path}/kp_http_request*" />
</bean>

<bean id="multiResourcePartitionerRes"
    class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
    <property name="resources" value="file:${app.file.path}/kp_http_response*" />
</bean>

<bean id="kpPool"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
    destroy-method="destroy">
    <property name="maxPoolSize" value="${app.max_thread_num}" />
</bean>

<bean id="idIncrementer1"
    class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

<batch:job id="kpGenJob" incrementer="idIncrementer1"
    job-repository="jobRepository">
    <batch:split id="splitStep" next="step4">
        <batch:flow>
            <batch:step id="step2">
                <batch:partition partitioner="multiResourcePartitionerReq"
                    step="step2.slave">
                    <batch:handler task-executor="kpPool" />
                </batch:partition>
            </batch:step>
        </batch:flow>
        <batch:flow>
            <batch:step id="step3">
                <batch:partition partitioner="multiResourcePartitionerRes"
                    step="step3.slave">
                    <batch:handler task-executor="kpPool" />
                </batch:partition>
            </batch:step>
        </batch:flow>
    </batch:split>
    <batch:step id="step4">
        <batch:tasklet transaction-manager="batchTransactionManager">
            <ref bean="kptasklet" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<batch:step id="step2.slave">
    <batch:tasklet transaction-manager="batchTransactionManager">
        <batch:chunk reader="reqItemReader" writer="cvsFileItemWriter"
            commit-interval="10000" />
    </batch:tasklet>
</batch:step>

<batch:step id="step3.slave">
    <batch:tasklet transaction-manager="batchTransactionManager">
        <batch:chunk reader="resItemReader" writer="cvsFileItemWriter"
            commit-interval="10000" />
    </batch:tasklet>
</batch:step>

<bean id="reqItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
    scope="step">

    <property name="resource" value="#{stepExecutionContext['fileName']}" />

    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <!-- split it -->
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="includedFields" value="5,6,8,10,11"></property>
                    <property name="names"
                        value="f1,f2,f3,f4,f5" />
                    <property name="strict" value="false" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean
                    class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="tblHttpData" />
                </bean>
            </property>
        </bean>
    </property>
    <property name="linesToSkip" value="1"></property>
</bean>

<bean id="tblHttpData" class="com.kp.batch.batch.job.domain.TblHttpData"
    scope="prototype" />

<bean id="resItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
    scope="step">

    <property name="resource" value="#{stepExecutionContext['fileName']}" />

    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <!-- split it -->
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="includedFields" value="3,4"></property>
                    <property name="names" value="f1,f2" />
                    <property name="strict" value="false" />
                </bean>
            </property>
            <property name="fieldSetMapper">

                <bean
                    class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="tblHttpData" />
                    <property name="strict" value="true" />
                </bean>
            </property>
        </bean>
    </property>
    <property name="linesToSkip" value="1"></property>
</bean>

<bean id="kptasklet" class="com.kp.batch.batch.job.step.KPTasklet" />

<bean id="cvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
    scope="step">
    <!-- write to this csv file -->
    <property name="resource" value="#{stepExecutionContext['fileName']}.tmp" />
    <property name="shouldDeleteIfExists" value="true" />

    <property name="lineAggregator">
        <bean
            class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter" value="," />

            <property name="fieldExtractor">
                <bean
                    class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names"
                        value="server,refferer,application,baseApplication,httpSize" />
                </bean>
            </property>
        </bean>
    </property>

</bean>

メインクラス

@SpringBootApplication
@EnableBatchProcessing
@ImportResource(value={"classpath:spring-context.xml"})
public class KPBootApplication {

    private final static Logger LOG = LoggerFactory
            .getLogger(KPBootApplication.class);

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(
                KPBootApplication.class, args);
        LOG.info("Application KPBOOT Started");
        SayHello hello = (SayHello) ctx.getBean("sayHello");
        if (hello != null) {
            LOG.debug("hello is not null");
            LOG.info("Got message {}", hello.getMessage());
        } else {
            LOG.debug("hello is null");
        }
        LOG.info("Done");

    }
}

コンテキスト構成アノテーションをコメント化し、@ SpringApplicationConfiguration(注:アノテーション@EnableBatchProcessingを使用してスプリングバッチを有効化)を有効にしてスプリングブートを有効にし、JobRepoなどのスプリングバッチ用にスプリングブートによって自動作成されたBeanをコメント化すると、次のようになりますステップスコープのコンテキストホルダーがないことを示すエラー

Java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.Java:94)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.Java:72)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.Java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.Java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.Java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.Java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.Java:252)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.Java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:217)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.Java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.Java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.Java:163)
    at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:50)
    at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:467)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:683)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:390)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2': Cannot resolve reference to bean 'step2.slave' while setting bean property 'step'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2.slave': Cannot resolve reference to bean 'reqItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': 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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.Java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.Java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:743)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:321)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.Java:98)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.Java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.Java:86)
    ... 25 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step2.slave': Cannot resolve reference to bean 'reqItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': 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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.Java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.Java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:351)
    ... 42 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reqItemReader': 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:352)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:351)
    ... 52 common frames omitted
Caused by: Java.lang.IllegalStateException: No context holder available for step scope
    at org.springframework.batch.core.scope.StepScope.getContext(StepScope.Java:160)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.Java:99)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:337)
    ... 54 common frames omitted

スプリングブートがエラーをスローする理由がわからない
注:spring-bootバージョン1.2.1

13
Karthik Prasad

これはバグである可能性があります(まだ調査中です)が、回避策はあります。この原因は、@EnableBatchProcessing自動的に構成されるStepScopeはJava構成を想定しているため、ステップスコープのBeanをプロキシしないため、作成が早すぎます。回避策は手動で構成することです。 XML構成でのStepScopeは、次の構成になっています。

<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
    <property name="autoProxy" value="true"/>
</bean>
11
Michael Minella

マイケルのコメントは私のために働いています、私は私のような怠惰な人々のためにJavaConfigコピー/ペーストの代替も提供しています:)

@Bean
public StepScope stepScope() {
    final StepScope stepScope = new StepScope();
    stepScope.setAutoProxy(true);
    return stepScope;
}
7
Gondy

@RunWith(SpringRunner.class)を使用しているのを見て、クラスの上で@TestExecutionListeners({..., StepScopeTestExecutionListener.class})を宣言すると、スコープが設定されます。

JobScopeの@TestExecutionListeners({..., JobScopeTestExecutionListener.class})と同じです。

3
coderatchet

2つの方法で解決できます。

A.

Application.propertiesでspring.main.allow-bean-definition-overriding=trueを使用します

B..

スプリングブートバッチスコープの問題を解決し、spring.main.allow-bean-definition-overriding=trueの使用を回避するには:

ソリューション:

StepScopeのautoProxyを無効にします。これを実行するには、次のコードスニペットを参照として使用します。

<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
      <property name="autoProxy" value="false"/>
</bean>

次のSpring Bean構成を実行します。

A.)

XMLベースの構成を介してステップスコープを構成するには:

 <bean id="userPreferences" class="com.foo.UserPreferences" scope="step">
   <aop:scoped-proxy/>
 </bean>

B.)

Javaベースの構成を介してステップスコープを構成するには:

@StepScope
@Component(value = "userPreferences")
public class UserPreference {}
1
Punit Tiwari

設定することを忘れないでください

spring.main.allow-bean-definition-overriding=true
1
Jadyer