简体   繁体   中英

Apache Camel - problem with error handling

I have a problem with error handling. If an exception occurs in the jrnRoute , it is processed by the exceptionHandler processor. This is OK. But the message which caused the exception still stays in the jrnQueue , is processed again and causes the error over and over again. Also I have a warning (see below) in logs. How to stop the infinite redelivery? I want to throw a message away in case of an error.

Camel configuration

    <camel:onException>
        <camel:exception>java.lang.Exception</camel:exception>
        <camel:redeliveryPolicy disableRedelivery="true" />
        <camel:process ref="exceptionHandler" />
        <camel:rollback markRollbackOnly="true" />
    </camel:onException>

    <camel:route id="translatorRoute">
        <camel:from ref="transactionsQueue" />
        <camel:process ref="messageTranslator" />
        <camel:inOnly ref="apfRequestQueue" />
    </camel:route>
    <camel:route id="jrnRoute">
        <camel:from ref="jrnQueue" />
        <camel:process ref="jrnProcessor" />
        <camel:stop />
    </camel:route>
</camel:camelContext>

Warning

11:15:00,141 WARN  [JmsMessageListenerContainer] Execution of JMS message listener failed, and no ErrorHandler has been set.
org.apache.camel.RuntimeCamelException: org.apache.camel.RollbackExchangeException: Intended rollback. Exchange[JmsMessage: [ObjectMessageImpl com.swiftmq.jms.ObjectMessageImpl@c84d9d
messageIndex = 5_2
messageId = [LazyUTF8String, s=ID:/172.26.214.11/5349789614428334512/10/0, buffer=[B@5f7fd8]
userId = [LazyUTF8String, s=null, buffer=[B@1c254aa]
clientId = null
timeStamp = 1316682898526
correlationId = null
replyTo = null
destination = jrnQueue@z4smq_4001
deliveryMode = 2
redelivered = true
deliveryCount = 8
type = null
expiration = 0
priority = 4
props = {...}
readOnly = true
sourceRouter = null
destRouter = null
destQueue = null array=[B@1447e6b cnt=1295]]
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1145)
    at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:108)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:560)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:243)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1058)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1050)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:947)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.camel.RollbackExchangeException: Intended rollback. Exchange[JmsMessage: [ObjectMessageImpl com.swiftmq.jms.ObjectMessageImpl@c84d9d
messageIndex = 5_2
messageId = [LazyUTF8String, s=ID:/172.26.214.11/5349789614428334512/10/0, buffer=[B@5f7fd8]
userId = [LazyUTF8String, s=null, buffer=[B@1c254aa]
clientId = null
timeStamp = 1316682898526
correlationId = null
replyTo = null
destination = jrnQueue@z4smq_4001
deliveryMode = 2
redelivered = true
deliveryCount = 8
type = null
expiration = 0
priority = 4
props = {...}
readOnly = true
sourceRouter = null
destRouter = null
destQueue = null array=[B@1447e6b cnt=1295]]

Yes the markRollbackOnly will cause the transaction manager to mark the TX as rollback, and therefore the JMS broker will keep the message in the queue, and redeliver it again. So remove that instead.

And do as Ben posted above, with handled true:

 <camel:onException>
        <camel:exception>java.lang.Exception</camel:exception>
        <camel:redeliveryPolicy disableRedelivery="true" />
        <camel:handled>
            <camel:constant>true</camel:constant>
        </camel:handled>

        <camel:process ref="exceptionHandler" />
 </camel:onException>

looks like markRollbackOnly="true" is the issue...

I usually do something like this to simply handle (prevent propagating back to caller), prevent retries and log error messages...

onException(Exception.class)
    .handled(true).maximumRedeliveries(0)
    .to("log:error processing message");

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