Spring Batchで複数ステップによる実行をさせようとしたときにエラー

Spring Batchで複数ステップによる実行をさせようとしたときにエラー

事象

Spring Batchで複数ステップによる実行をさせようとしたときにエラー
一つのジョブの定義xml内に、step1とstep2を作成し複数ステップを実行させようとした。

エラー内容

[2019/02/03 15:48:25] [main] [o.s.b.c.l.s.CommandLineJobRunner] [ERROR] Job Terminated in error: Configuration problem: The element [Job5.step02] is unreachable
Offending resource: class path resource [META-INF/jobs/job5/job5.xml]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: The element [Job5.step02] is unreachable
Offending resource: class path resource [META-INF/jobs/job5/job5.xml]
	at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
	at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
	at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:72)
	at org.springframework.batch.core.configuration.xml.AbstractFlowParser.doParse(AbstractFlowParser.java:169)
	at org.springframework.batch.core.configuration.xml.InlineFlowParser.doParse(InlineFlowParser.java:61)
	at org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser.parseInternal(AbstractSingleBeanDefinitionParser.java:85)
	at org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.parse(AbstractBeanDefinitionParser.java:61)
	at org.springframework.batch.core.configuration.xml.JobParser.doParse(JobParser.java:121)
	at org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser.parseInternal(AbstractSingleBeanDefinitionParser.java:85)
	at org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.parse(AbstractBeanDefinitionParser.java:61)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1411)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1401)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:172)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290)
	at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:592)

原因

xmlファイルの記述ミス。
step2の定義に対して、どのジョブの次に動作するかという設定をしていなかった。

定義XML

   <batch:job id="Job5" job-repository="jobRepository">
        <batch:step id="Job5.step01">
            <batch:tasklet transaction-manager="jobTransactionManager" ref="job5TaskletStep1" />
            <!-- 例外ハンドラ -->
            <batch:listeners>
                <batch:listener ref="exceptionHandleListener" />
            </batch:listeners>
        </batch:step>
        <batch:step id="Job5.step02">
            <batch:tasklet transaction-manager="jobTransactionManager" ref="job5TaskletStep2" />
            <!-- 例外ハンドラ -->
            <batch:listeners>
                <batch:listener ref="exceptionHandleListener" />
            </batch:listeners>
        </batch:step>
    </batch:job>

解決策

step1の設定のbatch:stepタグにnext=”step2″属性を指定する

   <batch:job id="Job5" job-repository="jobRepository">
        <batch:step id="Job5.step01" next="Job5.step02">
            <batch:tasklet transaction-manager="jobTransactionManager" ref="job5TaskletStep1" />
            <!-- 例外ハンドラ -->
            <batch:listeners>
                <batch:listener ref="exceptionHandleListener" />
            </batch:listeners>
        </batch:step>
        <batch:step id="Job5.step02">
            <batch:tasklet transaction-manager="jobTransactionManager" ref="job5TaskletStep2" />
            <!-- 例外ハンドラ -->
            <batch:listeners>
                <batch:listener ref="exceptionHandleListener" />
            </batch:listeners>
        </batch:step>
    </batch:job>

実行結果

[2019/02/03 15:57:20] [main] [o.s.b.c.l.s.SimpleJobLauncher] [INFO ] Job: [FlowJob: [name=Job5]] launched with the following parameters: [{text=Hello!, jsr_batch_run_id=81}]
[2019/02/03 15:57:20] [main] [o.s.b.c.j.SimpleStepHandler] [INFO ] Executing step: [Job5.step01]
[2019/02/03 15:57:20] [main] [i.s.b.c.l.ExceptionHandleListener] [INFO ] start job!:{text=Hello!, jsr_batch_run_id=81}
■Hello! Current step is step1
[2019/02/03 15:57:20] [main] [i.s.b.c.l.ExceptionHandleListener] [INFO ] end job!:{text=Hello!, jsr_batch_run_id=81}
[2019/02/03 15:57:20] [main] [o.s.b.c.j.SimpleStepHandler] [INFO ] Executing step: [Job5.step02]
[2019/02/03 15:57:20] [main] [i.s.b.c.l.ExceptionHandleListener] [INFO ] start job!:{text=Hello!, jsr_batch_run_id=81}
■Hello! Current step is step2
[2019/02/03 15:57:20] [main] [i.s.b.c.l.ExceptionHandleListener] [INFO ] end job!:{text=Hello!, jsr_batch_run_id=81}
[2019/02/03 15:57:20] [main] [o.s.b.c.l.s.SimpleJobLauncher] [INFO ] Job: [FlowJob: [name=Job5]] completed with the following parameters: [{text=Hello!, jsr_batch_run_id=81}] and the following status: [COMPLETED]

参考

BeanDefinitionParsingException: Configuration: The element [step2] is unreachable

エラー・バグ対処カテゴリの最新記事