'cannot handle exceptions in spring batch integration flow
I'm trying to set up a flow that handle exceptions thrown during batch execution (this job is executed through the flow startBatchFlow), but the program ends without copying the files to the error directory.
Could you suggest a solution to handle the errors thrown during the execution of the flow?
Here is my code :
@Value("${app.file.error}")
private String errorPath;
@Autowired
@Qualifier("errorChannel")
private PublishSubscribeChannel errorChannel;
@Bean
public IntegrationFlow startBatchFlow(@Qualifier("fileReaderChannel") MessageChannel fileReaderChannel) {
return IntegrationFlows
.from(fileReadingMessageSource(), c -> c.poller(Pollers.fixedDelay(period))) //
.channel(fileReaderChannel)
.handle(fileWritingMessageHandler())
.aggregate(agg -> agg.correlationExpression("payload != null")
.releaseStrategy(new ExpressionEvaluatingReleaseStrategy("size == 2"))
.expireGroupsUponCompletion(true)
)
.transform(fileMessageToJobRequest()) //
.handle(jobLaunchingMessageHandler()) //
.wireTap(sf -> {
sf.handle(jobExecution ->
log.info("job Execution payload " + jobExecution.getPayload()));
})
.split(messageSplitter)
.wireTap(sf -> {
sf.handle(jobExecution ->
log.info("Message after split payload =" + jobExecution.getPayload())); // to modify
})
.route(Message.class, message -> message.getPayload() != null,
mapping -> mapping.subFlowMapping(true, archiveFilesFlow())
)
.get();
}
@Bean
public IntegrationFlow errorHandlingFlow() {
return IntegrationFlows.from(errorChannel)
.handle(message -> System.out.println("@@@@@@@@@@@@@@@@@@@@@" + message.getPayload()))
.get();
}
this the stack trace:
Encountered an error executing step businessDataExcelFileStep in job generateOpcvmJob
java.lang.IllegalArgumentException: Unable to invoke method: [public void fr.project.sample.steps.DataItemReader.beforeStep(org.springframework.batch.core.StepExecution)] on object: [fr.project.sample.steps.BusinessDataItemReader@79d743e6] with arguments: [[StepExecution: id=1, version=1, name=businessDataExcelFileStep, status=STARTED, exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=0, exitDescription=]]
at org.springframework.batch.support.SimpleMethodInvoker.invokeMethod(SimpleMethodInvoker.java:112) ~[spring-batch-infrastructure-4.3.4.jar:4.3.4]
at org.springframework.batch.core.listener.MethodInvokerMethodInterceptor.invoke(MethodInvokerMethodInterceptor.java:69) ~[spring-batch-core-4.3.4.jar:4.3.4]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.15.jar:5.3.15]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.15.jar:5.3.15]
at jdk.proxy2/jdk.proxy2.$Proxy57.beforeStep(Unknown Source) ~[na:na]
2022-05-11 15:40:01.123 WARN 19196 --- [ scheduling-1] o.s.i.dispatcher.BroadcastingDispatcher : Suppressing Exception since 'ignoreFailures' is set to TRUE.
org.springframework.messaging.MessageHandlingException: failed to write Message payload to file in the [bean 'fileWritingMessageError' for component 'errorHandlerFlow.org.springframework.integration.config.ConsumerEndpointFactoryBean#0'; defined in: 'class path resource [fr/project/sample/config/FilesReaderIntegrationConfig.class]'; from source: 'fr.project.sample.config.FilesReaderIntegrationConfig.fileWritingMessageError()']; nested exception is java.lang.IllegalArgumentException: Unsupported Message payload type [org.springframework.messaging.MessageHandlingException]
at org.springframework.integration.support.utils.IntegrationUtils.wrapInHandlingExceptionIfNecessary(IntegrationUtils.java:191) ~[spring-integration-core-5.5.8.jar:5.5.8]
at org.springframework.integration.file.FileWritingMessageHandler.handleRequestMessage(FileWritingMessageHandler.java:543) ~[spring-integration-file-5.5.8.jar:5.5.8]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:136) ~[spring-integration-core-5.5.8.jar:5.5.8]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:56) ~[spring-integration-core-5.5.8.jar:5.5.8]
at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:222) ~[spring-integration-core-5.5.8.jar:5.5.8]
Edit my code :
Message errorMessage= MessageBuilder.withPayload(new ErrorMessage(new BusinessException("Business File not exist"))).build();
errorChannel.send(errorMessage);
after this modification (sending exception at the step '.transform(fileMessageToJobRequest())) ), i I get the message on the errorChannel. But the problem is that it launches the other steps of the flow or that it should stop and not go to the next steps handle(jobLaunchingMessageHandler( )).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|