Suppressing stack trace when catching exception

Nelda.techspiress

I have a short 2 step Spring Batch job that reads from an xml rest service call using StaxEventItemReader in Strict mode. I want it in strict mode because without the ability to connect to the rest service, there is no reason to continue. I want to catch the exception and exit gracefully with a custom message, not with a stack trace.

I'm able to catch the exception using a StepExecutionListener with afterStep() method checking the failures (stepExecution.getFailureExceptions()), but the stack trace is still being output. I'm getting my custom message (13:05:32,981 in the STS Output below) plus the stack trace. When I used the ItemReaderListener with onError method the onError method is not called, I'm guessing because it is an initialization exception not a read error.

What is the best method for capturing the failed to initialize reader exception and suppress the stack trace from the output?

SpringBatch Job:

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

STS output with stack trace:

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
	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
	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:497)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy8.open(Unknown Source)
	at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
	at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
	at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
	at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
	at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
	at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
	at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165)
	at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
	at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
	at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
	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 org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:362)
	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)
	at org.springframework.batch.item.xml.StaxEventItemReader.doOpen(StaxEventItemReader.java:195)
	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
	... 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.springframework.context.support.ClassPathXmlApplicationContext@4f4a7090: startup date [Wed Jul 01 13:05:29 EDT 2015]; root of context hierarchy

Luca Basso Ricci

When facing this problem probably the best solution is to check for REST service as soon as possible because "without the ability to connect to the rest service, there is no reason to continue [...] and exit gracefully with a custom message".
Before first step use a JobExecutionDecider where you check for REST availability and continue or end your job according to service presence/absence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exception that emails stack trace when raised

From Dev

Suppressing stack trace dump in Laravel log after App::abort(403)

From Dev

Suppressing stack trace dump in Laravel log after App::abort(403)

From Dev

No stack trace is printed when ScheduledExecutorService's task throws an exception

From Dev

Lost stack trace when exception is handelled with @ControllerAdvice annotated class

From Dev

Exception Stack Trace Missing Items

From Dev

Why is the exception stack trace not logged in?

From Dev

Suppressing ORA-06512 Exception stack information in oracle plsql

From Dev

Stack trace not providing exception source method when using async in Silverlight 5

From Dev

How can I make program print a stack trace automatically when an Exception occurs in Android

From Java

How can I get a JavaScript stack trace when I throw an exception?

From Dev

How to return the exception stack trace to normal, when using win32console gem?

From Dev

How do I see the full stack trace when I catch an exception

From Java

How to print the stack trace of an exception object in Python?

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

Rethrow an Exception with correct line numbers in the stack trace

From Dev

How can an Exception be created/thrown with no stack trace?

From Dev

logback: newline before exception stack trace but not otherwise?

From Dev

Exception stack trace and aggregation in Parallel.ForEach

From Dev

How to print the stack trace of an exception object in Python?

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

cxf null pointer exception without stack trace

From Dev

Printing the stack trace vs the exception itself

From Dev

How does the console obtain the stack trace of an exception?

From Dev

Why use IOexception instead of Exception when catching?

From Java

Get exception description and stack trace which caused an exception, all as a string

From Dev

Suppressing trace colors and legend in plotly's subplot

Related Related

  1. 1

    Exception that emails stack trace when raised

  2. 2

    Suppressing stack trace dump in Laravel log after App::abort(403)

  3. 3

    Suppressing stack trace dump in Laravel log after App::abort(403)

  4. 4

    No stack trace is printed when ScheduledExecutorService's task throws an exception

  5. 5

    Lost stack trace when exception is handelled with @ControllerAdvice annotated class

  6. 6

    Exception Stack Trace Missing Items

  7. 7

    Why is the exception stack trace not logged in?

  8. 8

    Suppressing ORA-06512 Exception stack information in oracle plsql

  9. 9

    Stack trace not providing exception source method when using async in Silverlight 5

  10. 10

    How can I make program print a stack trace automatically when an Exception occurs in Android

  11. 11

    How can I get a JavaScript stack trace when I throw an exception?

  12. 12

    How to return the exception stack trace to normal, when using win32console gem?

  13. 13

    How do I see the full stack trace when I catch an exception

  14. 14

    How to print the stack trace of an exception object in Python?

  15. 15

    locals() and globals() in stack trace on exception (Python)

  16. 16

    How to use a stack trace dump to debug an exception?

  17. 17

    Rethrow an Exception with correct line numbers in the stack trace

  18. 18

    How can an Exception be created/thrown with no stack trace?

  19. 19

    logback: newline before exception stack trace but not otherwise?

  20. 20

    Exception stack trace and aggregation in Parallel.ForEach

  21. 21

    How to print the stack trace of an exception object in Python?

  22. 22

    locals() and globals() in stack trace on exception (Python)

  23. 23

    How to use a stack trace dump to debug an exception?

  24. 24

    cxf null pointer exception without stack trace

  25. 25

    Printing the stack trace vs the exception itself

  26. 26

    How does the console obtain the stack trace of an exception?

  27. 27

    Why use IOexception instead of Exception when catching?

  28. 28

    Get exception description and stack trace which caused an exception, all as a string

  29. 29

    Suppressing trace colors and legend in plotly's subplot

HotTag

Archive