how to identify the exception if file is not found while Reading (Spring Batch) - spring-batch

I have just started working on Spring Batch (version 3.0.6). I have a job and inside step I'm having Reader->Processor->Writer.
How we can catch or identify the exception is thrown if file is not found while reading. In this case I want to trigger email.

You can use the StepExecutionListener to process the exception that is thrown by the ItemStream#open (as the opening of a file in the FlatFileItemReader is), sending your email from there. To get the exception that caused the failure, you can look at StepExecution#getFailureExceptions which will contain the exception that caused the step to fail.
You can read more about the StepExecutionListener in the javadoc here: https://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/StepExecutionListener.html

Related

Maven SpringBoot app starts up from mvn spring-boot:run, but fails with Websphere errors running Spring Boot from Eclipse

I work on quite a few services with similar architectures, with small differences. They all use Java 8, Maven, SpringBoot, and Jersey.
I normally debug them in Eclipse (currently on 2021-06) using "Run As->SpringBoot". This works perfectly fine. I can also run them from a command line using "mvn spring-boot:run", but that's just an academic exercise, because I prefer to run them from Eclipse.
When I run it from mvn, it successfully starts up, and I can hit listener endpoints (test case is actuator/info right now) with no problem.
When I run it from Eclipse, I get the following mystifying error:
BeanCreationException: Error creating bean with name 'mbeanExporter' defined in class path resource [org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jmx.export.annotation.AnnotationMBeanExporter]: Factory method 'mbeanExporter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mbeanServer' defined in class path resource [org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.management.MBeanServer]: Factory method 'mbeanServer' threw exception; nested exception is org.springframework.jmx.MBeanServerNotFoundException: Could not access WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method; nested exception is java.lang.NullPointerException
Notice the last couple of phrases in that.
This service uses an ejb client class that is configured to connect to Websphere EJBs. One side effect of this is that jmx is not available. I have to set "spring.jmx.enabled=false".
Note that I went to the trouble of storing the log file for both runs, and I painstakingly compared them, verifying that they were logging the same information (varying by timestamps). That stacktrace above is the first place where they truly vary.
Curiously, although the Eclipse run shows this error, and listeners do not respond, it doesn't terminate the startup attempt. The service just sits there, sort of brain-dead.
I'm sure what I've provided isn't enough information, but I'm not sure what else would be useful information.

How can i solve java.lang.IllegalStateException: Illegal access:?

I am using Eclipse to develope a webapp. Everything was working fine, but sometimes when I tried to publist->start my server, I received the following error:
Oct 03, 2019 6:52:55 PM org.apache.catalina.loader.WebappClassLoaderBase checkStateForResourceLoading
INFO: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1383)
at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1036)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkThreadContextClassLoader(AbandonedConnectionCleanupThread.java:117)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:84)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
For the first few times, I just restarted the server, and it was working fine, but now even a restart doesent solve the problem. I read about it online, and some sources mentioned that it might be a thread management issue, but I couldn't get further than that. Can anybody please provide some info? Thanks.
The mysql driver is trying to clean-up on web application shutdown which is good. Unfortunately it is trying to load additional classes to perform this clean-up and - since the web application is shutting down - class loading is no longer available. Ideally, the driver would load all the classes it needed to clean-up when it firsts loads - that would avoid this issue.
If you can figure out which classes it is trying to load - a check of the driver source code should show you which - then you should be able to load them yourself on application start in, for example, a ServletContextListener.
If you created any Entity class and forgot to give ID to any specific column then also this problem occurs. So assign #ID to any column and run again. your issue will resolve.

scalaXB Async Dispatcher java.lang.IllegalStateException: failed to create a child event loop

I am using ScalaXB to make a call to a SOAP Web Service. I generated the code against WSDL using the following command
I am making a call to the web service in a loop. I notice that the code runs fine for sometime but then starts throwing the following error
java.lang.IllegalStateException: failed to create a child event loop
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:68)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:50)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:70)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:65)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:56)
at org.asynchttpclient.netty.channel.ChannelManager.<init>(ChannelManager.java:173)
at org.asynchttpclient.DefaultAsyncHttpClient.<init>(DefaultAsyncHttpClient.java:85)
at dispatch.Http.client$lzycompute(execution.scala:16)
at dispatch.Http.client(execution.scala:16)
at dispatch.Http.client(execution.scala:11)
at dispatch.HttpExecutor$class.apply(execution.scala:120)
at dispatch.Http.apply(execution.scala:11)
at dispatch.HttpExecutor$class.apply(execution.scala:115)
at dispatch.Http.apply(execution.scala:11)
at scalaxb.DispatchHttpClientsAsync$DispatchHttpClient$class.request(httpclients_dispatch_async.scala:22)
at scalaxb.DispatchHttpClientsAsync$$anon$1.request(httpclients_dispatch_async.scala:7)
at scalaxb.SoapClientsAsync$SoapClientAsync$class.soapRequest(soap12_async.scala:43)
at scalaxb.SoapClientsAsync$$anon$1.soapRequest(soap12_async.scala:24)
at scalaxb.SoapClientsAsync$SoapClientAsync$class.requestResponse(soap12_async.scala:63)
at scalaxb.SoapClientsAsync$$anon$1.requestResponse(soap12_async.scala:24)
Caused by: io.netty.channel.ChannelException: failed to open a new selector
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:176)
at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:150)
at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:103)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:64)
... 34 more
Caused by: java.io.IOException: Too many open files in system
at sun.nio.ch.KQueueArrayWrapper.init(Native Method)
at sun.nio.ch.KQueueArrayWrapper.<init>(KQueueArrayWrapper.java:98)
at sun.nio.ch.KQueueSelectorImpl.<init>(KQueueSelectorImpl.java:88)
at sun.nio.ch.KQueueSelectorProvider.openSelector(KQueueSelectorProvider.java:42)
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:174)
... 37 more
I found out the answer to this question. The problem was that I was doing a new on the proxy object inside of the loop.
It seems that these objects create a new connection and don't get cleaned up resulting in the error above.
I moved the proxy object out of the loop and just called the webservice inside the loop and the problem got resolved.

Spring Batch - How to keep job running when exception occur?

I try to write example app using spring batch:
#Bean
public Step testStep() {
return steps.get("testStep").<String,String>chunk(1)
.reader(testReader())
.processor(testProcessor())
.writer(testWriter())
.listener(testListener())
.build();
}
When I throw exception in reader or processor or writer, the job stopped with status FAILED. How can I make job ignore exception and keep running.
I'm not use any xml config, just annotation and class. Please give me a hint or link.
Thanks for any support!
Edit: Can we add skip dynamically, like I post in answer below.

Spring GroovyDynamicElementReader tries to read a Groovy script as XML and fails

When I looked at Spring Boot, I liked the idea that it:
Gets out of the way quickly if you want to change defaults. - Dave Syer, Spring One 2GX 2013
It wasn't long before practicality hit me. While trying to make Boot work with a 100% Groovy/no-XML pet project, I found 2 issues in 2 days. Here's one of them.
Groovy config file:
beans {
xmlns([ctx:'http://www.springframework.org/schema/context'])
ctx.'component-scan'('base-package':'name.abhijitsarkar.moviedatabase')
}
Exception:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read XML document; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unable to determine validation mode for [Groovy]: cannot open InputStream. Did you attempt to load directly from a SAX InputSource without specifying the validationMode on your XmlBeanDefinitionReader instance?; nested exception is java.io.FileNotFoundException: Groovy cannot be opened because it does not point to a readable resource
at org.springframework.beans.factory.xml.XmlReaderContext.readDocumentFromString(XmlReaderContext.java:98)
at org.springframework.beans.factory.xml.XmlReaderContext$readDocumentFromString.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.springframework.beans.factory.groovy.GroovyDynamicElementReader.invokeMethod(GroovyDynamicElementReader.groovy:96)
Also posted on Spring forum.