org.apache.camel.processor
Class RedeliveryErrorHandler

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.support.ChildServiceSupport
          extended by org.apache.camel.processor.ErrorHandlerSupport
              extended by org.apache.camel.processor.RedeliveryErrorHandler
All Implemented Interfaces:
AsyncProcessor, Processor, ErrorHandler, Service, ShutdownableService, StatefulService, SuspendableService
Direct Known Subclasses:
DeadLetterChannel, DefaultErrorHandler

public abstract class RedeliveryErrorHandler
extends ErrorHandlerSupport
implements AsyncProcessor

Base redeliverable error handler that also supports a final dead letter queue in case all redelivery attempts fail.

This implementation should contain all the error handling logic and the sub classes should only configure it according to what they support.

Version:

Nested Class Summary
protected  class RedeliveryErrorHandler.RedeliveryData
          Contains the current redelivery data
 
Field Summary
protected  CamelContext camelContext
           
protected  Processor deadLetter
           
protected  String deadLetterUri
           
protected  ScheduledExecutorService executorService
           
protected  CamelLogger logger
           
protected  Processor output
           
protected  AsyncProcessor outputAsync
           
protected  boolean redeliveryEnabled
           
protected  RedeliveryPolicy redeliveryPolicy
           
protected  Processor redeliveryProcessor
           
protected  Predicate retryWhilePolicy
           
protected  boolean useOriginalMessagePolicy
           
 
Fields inherited from class org.apache.camel.processor.ErrorHandlerSupport
exceptionPolicies, exceptionPolicy, log
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
RedeliveryErrorHandler(CamelContext camelContext, Processor output, CamelLogger logger, Processor redeliveryProcessor, RedeliveryPolicy redeliveryPolicy, Processor deadLetter, String deadLetterUri, boolean useOriginalMessagePolicy, Predicate retryWhile, ScheduledExecutorService executorService)
           
 
Method Summary
protected  Exchange defensiveCopyExchangeIfNeeded(Exchange exchange)
          Performs a defensive copy of the exchange if needed
protected  boolean deliverToFailureProcessor(Processor processor, boolean isDeadLetterChannel, Exchange exchange, RedeliveryErrorHandler.RedeliveryData data, AsyncCallback callback)
          All redelivery attempts failed so move the exchange to the dead letter queue
protected  void deliverToOnRedeliveryProcessor(Exchange exchange, RedeliveryErrorHandler.RedeliveryData data)
          Gives an optional configure redelivery processor a chance to process before the Exchange will be redelivered.
protected  long determineRedeliveryDelay(Exchange exchange, RedeliveryPolicy redeliveryPolicy, long redeliveryDelay, int redeliveryCounter)
          Determines the redelivery delay time by first inspecting the Message header Exchange.REDELIVERY_DELAY and if not present, defaulting to RedeliveryPolicy.calculateRedeliveryDelay(long, int)
protected  void doShutdown()
          Implementations override this method to perform customized shutdown.
protected  void doStart()
          Implementations override this method to support customized start/stop.
protected  void doStop()
          Implementations override this method to support customized start/stop.
 Processor getDeadLetter()
          Returns the dead letter that message exchanges will be sent to if the redelivery attempts fail
 String getDeadLetterUri()
           
protected  Predicate getDefaultHandledPredicate()
           
 CamelLogger getLogger()
           
 Processor getOutput()
          Returns the output processor
 RedeliveryPolicy getRedeliveryPolicy()
           
protected  void handleException(Exchange exchange, RedeliveryErrorHandler.RedeliveryData data)
           
protected  boolean isCancelledOrInterrupted(Exchange exchange)
          Strategy to determine if the exchange was cancelled or interrupted
protected  boolean isDone(Exchange exchange)
          Strategy to determine if the exchange is done so we can continue
 boolean isRunAllowed()
          Helper methods so the service knows if it should keep running.
 boolean isUseOriginalMessagePolicy()
           
protected  void prepareExchangeAfterFailure(Exchange exchange, RedeliveryErrorHandler.RedeliveryData data, boolean shouldHandle, boolean shouldContinue)
           
protected  void prepareExchangeForContinue(Exchange exchange, RedeliveryErrorHandler.RedeliveryData data)
           
protected  void prepareExchangeForRedelivery(Exchange exchange, RedeliveryErrorHandler.RedeliveryData data)
           
 void process(Exchange exchange)
          Processes the message exchange
 boolean process(Exchange exchange, AsyncCallback callback)
          Processes the message exchange.
protected  void processAsyncErrorHandler(Exchange exchange, AsyncCallback callback, RedeliveryErrorHandler.RedeliveryData data)
          This logic is only executed if we have to retry redelivery asynchronously, which have to be done from the callback.
protected  boolean processErrorHandler(Exchange exchange, AsyncCallback callback, RedeliveryErrorHandler.RedeliveryData data)
          Process the exchange using redelivery error handling.
protected  boolean shouldHandleException(Exchange exchange)
          Strategy whether the exchange has an exception that we should try to handle.
 boolean supportTransacted()
          Whether this error handler supports transacted exchanges or not.
 
Methods inherited from class org.apache.camel.processor.ErrorHandlerSupport
addExceptionPolicy, createDefaultExceptionPolicyStrategy, getExceptionPolicy, isDeadLetterChannel, setExceptionPolicy
 
Methods inherited from class org.apache.camel.support.ChildServiceSupport
addChildService, removeChildService, shutdown, start, start, stop
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doSuspend, getStatus, getVersion, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executorService

protected ScheduledExecutorService executorService

camelContext

protected final CamelContext camelContext

deadLetter

protected final Processor deadLetter

deadLetterUri

protected final String deadLetterUri

output

protected final Processor output

outputAsync

protected final AsyncProcessor outputAsync

redeliveryProcessor

protected final Processor redeliveryProcessor

redeliveryPolicy

protected final RedeliveryPolicy redeliveryPolicy

retryWhilePolicy

protected final Predicate retryWhilePolicy

logger

protected final CamelLogger logger

useOriginalMessagePolicy

protected final boolean useOriginalMessagePolicy

redeliveryEnabled

protected boolean redeliveryEnabled
Constructor Detail

RedeliveryErrorHandler

public RedeliveryErrorHandler(CamelContext camelContext,
                              Processor output,
                              CamelLogger logger,
                              Processor redeliveryProcessor,
                              RedeliveryPolicy redeliveryPolicy,
                              Processor deadLetter,
                              String deadLetterUri,
                              boolean useOriginalMessagePolicy,
                              Predicate retryWhile,
                              ScheduledExecutorService executorService)
Method Detail

supportTransacted

public boolean supportTransacted()
Description copied from class: ErrorHandlerSupport
Whether this error handler supports transacted exchanges or not.

Specified by:
supportTransacted in class ErrorHandlerSupport

isRunAllowed

public boolean isRunAllowed()
Description copied from interface: StatefulService
Helper methods so the service knows if it should keep running. Returns false if the service is being stopped or is stopped.

Specified by:
isRunAllowed in interface StatefulService
Overrides:
isRunAllowed in class ServiceSupport
Returns:
true if the service should continue to run.

process

public void process(Exchange exchange)
             throws Exception
Description copied from interface: Processor
Processes the message exchange

Specified by:
process in interface Processor
Parameters:
exchange - the message exchange
Throws:
Exception - if an internal processing error has occurred.

process

public boolean process(Exchange exchange,
                       AsyncCallback callback)
Description copied from interface: AsyncProcessor
Processes the message exchange. Similar to Processor.process(org.apache.camel.Exchange), but the caller supports having the exchange asynchronously processed.

If there was a failure processing then the caused Exception would be set on the Exchange.

Specified by:
process in interface AsyncProcessor
Parameters:
exchange - the message exchange
callback - the AsyncCallback will be invoked when the processing of the exchange is completed. If the exchange is completed synchronously, then the callback is also invoked synchronously. The callback should therefore be careful of starting recursive loop.
Returns:
(doneSync) true to continue execute synchronously, false to continue being executed asynchronously
See Also:
AsyncProcessorHelper.process(AsyncProcessor, Exchange, AsyncCallback)

processErrorHandler

protected boolean processErrorHandler(Exchange exchange,
                                      AsyncCallback callback,
                                      RedeliveryErrorHandler.RedeliveryData data)
Process the exchange using redelivery error handling.


determineRedeliveryDelay

protected long determineRedeliveryDelay(Exchange exchange,
                                        RedeliveryPolicy redeliveryPolicy,
                                        long redeliveryDelay,
                                        int redeliveryCounter)

Determines the redelivery delay time by first inspecting the Message header Exchange.REDELIVERY_DELAY and if not present, defaulting to RedeliveryPolicy.calculateRedeliveryDelay(long, int)

In order to prevent manipulation of the RedeliveryData state, the values of RedeliveryErrorHandler.RedeliveryData.redeliveryDelay and RedeliveryErrorHandler.RedeliveryData.redeliveryCounter are copied in.

Parameters:
exchange - The current exchange in question.
redeliveryPolicy - The RedeliveryPolicy to use in the calculation.
redeliveryDelay - The default redelivery delay from RedeliveryData
redeliveryCounter - The redeliveryCounter
Returns:
The time to wait before the next redelivery.

processAsyncErrorHandler

protected void processAsyncErrorHandler(Exchange exchange,
                                        AsyncCallback callback,
                                        RedeliveryErrorHandler.RedeliveryData data)
This logic is only executed if we have to retry redelivery asynchronously, which have to be done from the callback.

And therefore the logic is a bit different than the synchronous processErrorHandler method which can use a loop based redelivery technique. However this means that these two methods in overall have to be in sync in terms of logic.


defensiveCopyExchangeIfNeeded

protected Exchange defensiveCopyExchangeIfNeeded(Exchange exchange)
Performs a defensive copy of the exchange if needed

Parameters:
exchange - the exchange
Returns:
the defensive copy, or null if not needed (redelivery is not enabled).

shouldHandleException

protected boolean shouldHandleException(Exchange exchange)
Strategy whether the exchange has an exception that we should try to handle.

Standard implementations should just look for an exception.


isDone

protected boolean isDone(Exchange exchange)
Strategy to determine if the exchange is done so we can continue


isCancelledOrInterrupted

protected boolean isCancelledOrInterrupted(Exchange exchange)
Strategy to determine if the exchange was cancelled or interrupted


getOutput

public Processor getOutput()
Returns the output processor

Specified by:
getOutput in class ErrorHandlerSupport

getDeadLetter

public Processor getDeadLetter()
Returns the dead letter that message exchanges will be sent to if the redelivery attempts fail


getDeadLetterUri

public String getDeadLetterUri()

isUseOriginalMessagePolicy

public boolean isUseOriginalMessagePolicy()

getRedeliveryPolicy

public RedeliveryPolicy getRedeliveryPolicy()

getLogger

public CamelLogger getLogger()

getDefaultHandledPredicate

protected Predicate getDefaultHandledPredicate()

prepareExchangeForContinue

protected void prepareExchangeForContinue(Exchange exchange,
                                          RedeliveryErrorHandler.RedeliveryData data)

prepareExchangeForRedelivery

protected void prepareExchangeForRedelivery(Exchange exchange,
                                            RedeliveryErrorHandler.RedeliveryData data)

handleException

protected void handleException(Exchange exchange,
                               RedeliveryErrorHandler.RedeliveryData data)

deliverToOnRedeliveryProcessor

protected void deliverToOnRedeliveryProcessor(Exchange exchange,
                                              RedeliveryErrorHandler.RedeliveryData data)
Gives an optional configure redelivery processor a chance to process before the Exchange will be redelivered. This can be used to alter the Exchange.


deliverToFailureProcessor

protected boolean deliverToFailureProcessor(Processor processor,
                                            boolean isDeadLetterChannel,
                                            Exchange exchange,
                                            RedeliveryErrorHandler.RedeliveryData data,
                                            AsyncCallback callback)
All redelivery attempts failed so move the exchange to the dead letter queue


prepareExchangeAfterFailure

protected void prepareExchangeAfterFailure(Exchange exchange,
                                           RedeliveryErrorHandler.RedeliveryData data,
                                           boolean shouldHandle,
                                           boolean shouldContinue)

doStart

protected void doStart()
                throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: See ServiceSupport.doStop() for more details.

Specified by:
doStart in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStop()

doStop

protected void doStop()
               throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: Camel will invoke this ServiceSupport.doStop() method when the service is being stopped. This method will also be invoked if the service is still in uninitialized state (eg has not been started). The method is always called to allow the service to do custom logic when the service is being stopped, such as when CamelContext is shutting down.

Specified by:
doStop in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStart()

doShutdown

protected void doShutdown()
                   throws Exception
Description copied from class: ServiceSupport
Implementations override this method to perform customized shutdown.

Overrides:
doShutdown in class ServiceSupport
Throws:
Exception


Apache CAMEL