简体   繁体   中英

Spring Batch - Recovery after Error

I have a Spring Batch Job with a ItemProcessor that can possibly fail with an Exception. If that happens I have an ItemProcessListener in place that handles the recovery (marking the item as failed so that it would not be picked up in the next run).

That solution works like a charm with 'regular' exceptions, however I've experienced a java.lang.OutOfMemoryError: Java heap space error recently where onProcessError() is not called and the item will not be excluded in the next run.

I noticed however that JobExecutionListener.afterJob() is called where I could get the job parameters of the failed execution and query all items of that execution again and mark them as failed.

Is there a better approach to my problem?

Basically onProcessError is ill-named. It only listens for Exception s, not for Error s. Java strongly discourages handling Error s via catch-blocks. The reason is, that the jvm signals a fault which is not in the programming but in the configuration of the vm via an Error . They should be taken as a strong indicator, your vm-setup needs correction.

In your case, you write about an OutOfMemoryError which occurs if your memory demand is higher than the memory, which has been assigned to the Java-job via -Xmx/Xms startparameters. You either assign to little memory or you consume too much of it. This should be fixed, not caught via some technical trick.

First of all, you should find out what causes this error. Maybe you simply need to assign more memory, probably some of your algorithms are too greedy in memory consumption and not unlikely, you have a memory leak as a root cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM