Spring batch exit message on workflow failure

Viewed 177

I have a Spring batch step executing a tasklet that polls for files on a remote server:

public class MyConfiguration extends AbstractConfiguration {
   
    @Bean
    @Qualifier("pollStep")
    public Step pollStep() {
        return stepBuilderFactory.get("pollStep")
                                 .tasklet(filePollingTasklet())
                                 .listener(promoteContextListener())
                                 .build();
    }

    @Bean
    @StepScope
    private Tasklet filePollingTasklet() {
        return ((stepContribution, chunkContext) -> getStatus(stepContribution, chunkContext));
    }

    private RepeatStatus getStatus(StepContribution stepContribution, ChunkContext chunkContext) {
        //some code
        Map<String, Boolean> result = poller.pollForFile(myContext, sourceInfo);
        return RepeatStatus.FINISHED;
    }

}

It polls for up to 100 minutes, retrying every so often...

When it is out of while loop polling for file in pollForFile() method, it throws a RuntimeException if it cannot find the file.

the spring batch STEP EXECUTION table records the EXIT MESSAGE for this step with exception as follows when the batch fails in this instance:

org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is org.hibernate.TransactionException: Unable to rollback against JDBC Connectionat org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:565)at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:835)at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:809)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:498)at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)at com.sun.proxy.$Proxy177.rollback(Unknown Source)at org.springframework.transaction.support.TransactionTemplate.rollbackOnException(TransactionTemplate.java:168)at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:144)at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208)at org.spring

I would like to understand why the exit message is a jpa rollback error and not the runtime exception? How can I ensure that in this case the runtime exception message is what gets stored in Exit Message in spring batch table when the step fails.

1 Answers

I would like to understand why the exit message is a jpa rollback error and not the runtime exception?

Because this is what actually makes your step fails. The stack trace you shared is truncated, but the runtime exception should be the cause of org.springframework.transaction.TransactionSystemException, which in turn is the cause of your step failure.

Related