2015-07-01 136 views
0

我有一個簡短的2步Spring Batch作業,它以嚴格模式使用StaxEventItemReader從xml rest服務調用中讀取。我希望它在嚴格的模式下,因爲沒有連接到其他服務的能力,沒有理由繼續。我想捕捉異常並用自定義消息正常退出,而不是使用堆棧跟蹤。捕捉異常時抑制堆棧跟蹤

我能夠使用StepExecutionListener和afterStep()方法檢查失敗(stepExecution.getFailureExceptions())來捕獲異常,但堆棧跟蹤仍在輸出。我收到我的自定義消息(下面的STS輸出中的13:05:32,981)以及堆棧跟蹤。當我用onError方法使用ItemReaderListener時,onError方法沒有被調用,我猜測是因爲它是一個初始化異常而不是讀取錯誤。

什麼是捕獲的最佳方法未能初始化讀取器異常並從輸出中抑制堆棧跟蹤?

SpringBatch工作:

與堆棧跟蹤

\t <batch:job id="randomSends2NG"> 
 
\t \t <batch:step id="getStations" next="generateAndSend"> 
 
\t \t \t <batch:tasklet> 
 
\t \t \t \t <batch:chunk reader="getStationsFromNG_RestXML" 
 
\t \t \t \t \t \t \t processor="idExtractor" 
 
\t \t \t \t \t \t \t writer="stationsWriter" 
 
\t \t \t \t \t \t \t commit-interval="1"> 
 
\t \t \t \t </batch:chunk> \t 
 
\t \t \t \t <batch:listeners> 
 
\t \t \t \t \t <batch:listener ref="noStationsStopListener" /> 
 
\t \t \t \t </batch:listeners> \t \t 
 
\t \t \t \t 
 
\t \t \t </batch:tasklet> 
 
\t \t </batch:step> 
 
\t \t <batch:step id="generateAndSend"> 
 
\t \t \t <batch:tasklet> 
 
\t \t \t \t <batch:chunk reader="readStationIdsList" 
 
\t \t \t \t \t \t \t processor="createRandomRequests" 
 
\t \t \t \t \t \t \t writer="ngBroadcasterService" 
 
\t \t \t \t \t \t \t commit-interval="400"> 
 
\t \t \t \t </batch:chunk> \t \t \t 
 
\t \t \t </batch:tasklet> 
 
\t \t </batch:step> 
 
\t </batch:job>

STS輸出:

13:05:30,479 [main] INFO SimpleJobLauncher - No TaskExecutor has been set, defaulting to synchronous executor. 
 
13:05:30,776 [main] INFO DepartmentSendCountsNoOpReader - constructing stationSendCountsReader 
 
LightYellow color:43 
 
13:05:31,368 [main] INFO ProcessStatisticsTasklet - constructor... 
 
13:05:31,525 [main] INFO RequestsListReader - constructing requestsListReader 
 
13:05:31,588 [main] INFO Jaxb2Marshaller - Creating JAXBContext with classes to be bound [class com.pevco.pevcotubesystem.StationConfig] 
 
13:05:31,886 [main] INFO SimpleJobLauncher - Job: [FlowJob: [name=randomSends2NG]] launched with the following parameters: [{hostNameOrIP=192.168.100.10:8383}] 
 
13:05:31,902 [main] INFO SimpleStepHandler - Executing step: [getStations] 
 
13:05:32,981 [main] ERROR AbstractStep - Encountered an error executing step getStations in job randomSends2NG 
 
org.springframework.batch.item.ItemStreamException: Failed to initialize the reader 
 
\t at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
 
\t at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
 
\t at java.lang.reflect.Method.invoke(Method.java:497) 
 
\t at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 
 
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
 
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
 
\t at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131) 
 
\t at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119) 
 
\t at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
 
\t at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
 
\t at com.sun.proxy.$Proxy8.open(Unknown Source) 
 
\t at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96) 
 
\t at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310) 
 
\t at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195) 
 
\t at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) 
 
\t at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) 
 
\t at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) 
 
\t at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165) 
 
\t at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) 
 
\t at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) 
 
\t at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304) 
 
\t at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) 
 
\t at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) 
 
\t at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) 
 
\t at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:362) 
 
\t at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590) 
 
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode) 
 
\t at org.springframework.batch.item.xml.StaxEventItemReader.doOpen(StaxEventItemReader.java:195) 
 
\t at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) 
 
\t ... 27 more 
 
13:05:32,981 [main] ERROR NoStationsStopListener - Not able to get stations list from NG. Check that NG is running 
 
13:05:32,998 [main] INFO SimpleJobLauncher - Job: [FlowJob: [name=randomSends2NG]] completed with the following parameters: [{hostNameOrIP=192.168.100.10:8383}] and the following status: [FAILED] 
 
13:05:32,998 [main] INFO ClassPathXmlApplicationContext - Closing org[email protected]4f4a7090: startup date [Wed Jul 01 13:05:29 EDT 2015]; root of context hierarchy

回答

1

當面臨這樣的proble m可能是最好的解決方案是儘快檢查REST服務,因爲「無法連接到其他服務,因此沒有理由繼續並使用自定義消息」「正常退出。
在第一步使用JobExecutionDecider之前,請檢查REST可用性,並根據服務存在/不存在來繼續或結束作業。