Search code examples
javaspringexceptionchain

Spring exception-type-router in a chain


I have the following chain in Spring:

<int:chain input-channel="errorRecoveryChannel">
  <int:transformer ref="exceptionLogger" method="logError"/>
  <int:transformer ref="errorHandlerTransformer" method="handleError"/>
  <int:header-value-router header-name="onErrorDestinationQueueChannel"
                           default-output-channel="reprocessOutChannel">
     <int:mapping value="dlqMessageOut" channel="dlqMessageOut"/>
  </int:header-value-router>
</int:chain>

And I have a class that throws a DemoClientNotAssociatedWithLiveClientDropMessageError custom exception.

Will something like this work?

<int:chain input-channel="errorRecoveryChannel">
  <int:transformer ref="exceptionLogger" method="logError"/>
  <int:exception-type-router>
     <int:mapping exception-type="com.iggroup.mis.gateway.errorhandling.DemoClientNotAssociatedWithLiveClientDropMessageError"
             channel="nullChannel"/>
  </int:exception-type-router>
  <int:transformer ref="errorHandlerTransformer" method="handleError"/>
  <int:header-value-router header-name="onErrorDestinationQueueChannel"
                           default-output-channel="reprocessOutChannel">
     <int:mapping value="dlqMessageOut" channel="dlqMessageOut"/>
  </int:header-value-router>
</int:chain>

Or do I have to add the exception-type-router outside the chain?


Solution

  • So I have found the answer by myself. Turns out the exception-type-router needs to be out of the chain. Here's the solution:

    <int:exception-type-router input-channel="errorChannel" default-output-channel="errorRecoveryChannel">
      <int:mapping exception-type="com.iggroup.mis.gateway.errorhandling.DemoClientNotAssociatedWithLiveClientDropMessageError"
                   channel="nullChannel"/>
    </int:exception-type-router>
    
    <int:chain input-channel="errorRecoveryChannel">
      <int:transformer ref="exceptionLogger" method="logError"/>
      <int:transformer ref="errorHandlerTransformer" method="handleError"/>
      <int:header-value-router header-name="onErrorDestinationQueueChannel"
                               default-output-channel="reprocessOutChannel">
         <int:mapping value="dlqMessageOut" channel="dlqMessageOut"/>
      </int:header-value-router>
    </int:chain>
    

    The way it was before, I got the following exception:

    java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.handler.MessageHandlerChain#4': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: All handlers except for the last one in the chain must implement the MessageProducer interface.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1554)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:102)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 28 more
    
    Caused by: java.lang.IllegalArgumentException: All handlers except for the last one in the chain must implement the MessageProducer interface.
    at org.springframework.util.Assert.isTrue(Assert.java:65)
    at org.springframework.integration.handler.MessageHandlerChain.configureChain(MessageHandlerChain.java:140)
    at org.springframework.integration.handler.MessageHandlerChain.onInit(MessageHandlerChain.java:116)
    at org.springframework.integration.context.IntegrationObjectSupport.afterPropertiesSet(IntegrationObjectSupport.java:133)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
    ... 43 more