org.apache.camel.processor.aggregate
Class AggregateProcessor

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.processor.aggregate.AggregateProcessor
All Implemented Interfaces:
Navigate<Processor>, Processor, Service, ShutdownableService, ShutdownPrepared, StatefulService, SuspendableService, Traceable

public class AggregateProcessor
extends ServiceSupport
implements Processor, Navigate<Processor>, Traceable, ShutdownPrepared

An implementation of the Aggregator pattern where a batch of messages are processed (up to a maximum amount or until some timeout is reached) and messages for the same correlation key are combined together using some kind of AggregationStrategy (by default the latest message is used) to compress many message exchanges into a smaller number of exchanges.

A good example of this is stock market data; you may be receiving 30,000 messages/second and you may want to throttle it right down so that multiple messages for the same stock are combined (or just the latest message is used and older prices are discarded). Another idea is to combine line item messages together into a single invoice message.


Field Summary
static String AGGREGATE_TIMEOUT_CHECKER
           
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
AggregateProcessor(CamelContext camelContext, Processor processor, Expression correlationExpression, AggregationStrategy aggregationStrategy, ExecutorService executorService, boolean shutdownExecutorService)
           
 
Method Summary
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.
 int forceCompletionOfAllGroups()
           
 AggregationRepository getAggregationRepository()
           
 Integer getCloseCorrelationKeyOnCompletion()
           
 long getCompletionInterval()
           
 Predicate getCompletionPredicate()
           
 int getCompletionSize()
           
 Expression getCompletionSizeExpression()
           
 long getCompletionTimeout()
           
 Expression getCompletionTimeoutExpression()
           
 ExceptionHandler getExceptionHandler()
           
 ScheduledExecutorService getTimeoutCheckerExecutorService()
           
 String getTraceLabel()
          Gets the trace label used for logging when tracing is enabled.
 boolean hasNext()
          Are there more outputs?
protected  String isCompleted(String key, Exchange exchange)
          Tests whether the given exchange is complete or not
 boolean isCompletionFromBatchConsumer()
           
 boolean isDiscardOnCompletionTimeout()
           
 boolean isEagerCheckCompletion()
           
 boolean isIgnoreInvalidCorrelationKeys()
           
 boolean isParallelProcessing()
           
 boolean isShutdownTimeoutCheckerExecutorService()
           
 List<Processor> next()
          Next group of outputs
protected  Exchange onAggregation(Exchange oldExchange, Exchange newExchange)
           
protected  void onCompletion(String key, Exchange exchange, boolean fromTimeout)
           
 void prepareShutdown(boolean forced)
          Prepares for shutdown.
 void process(Exchange exchange)
          Processes the message exchange
protected  void restoreTimeoutMapFromAggregationRepository()
          Restores the timeout map with timeout values from the aggregation repository.
 void setAggregationRepository(AggregationRepository aggregationRepository)
           
 void setCloseCorrelationKeyOnCompletion(Integer closeCorrelationKeyOnCompletion)
           
 void setCompletionFromBatchConsumer(boolean completionFromBatchConsumer)
           
 void setCompletionInterval(long completionInterval)
           
 void setCompletionPredicate(Predicate completionPredicate)
           
 void setCompletionSize(int completionSize)
           
 void setCompletionSizeExpression(Expression completionSizeExpression)
           
 void setCompletionTimeout(long completionTimeout)
           
 void setCompletionTimeoutExpression(Expression completionTimeoutExpression)
           
 void setDiscardOnCompletionTimeout(boolean discardOnCompletionTimeout)
           
 void setEagerCheckCompletion(boolean eagerCheckCompletion)
           
 void setExceptionHandler(ExceptionHandler exceptionHandler)
           
 void setForceCompletionOnStop(boolean forceCompletionOnStop)
           
 void setIgnoreInvalidCorrelationKeys(boolean ignoreInvalidCorrelationKeys)
           
 void setParallelProcessing(boolean parallelProcessing)
           
 void setShutdownTimeoutCheckerExecutorService(boolean shutdownTimeoutCheckerExecutorService)
           
 void setTimeoutCheckerExecutorService(ScheduledExecutorService timeoutCheckerExecutorService)
           
 String toString()
           
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

AGGREGATE_TIMEOUT_CHECKER

public static final String AGGREGATE_TIMEOUT_CHECKER
See Also:
Constant Field Values
Constructor Detail

AggregateProcessor

public AggregateProcessor(CamelContext camelContext,
                          Processor processor,
                          Expression correlationExpression,
                          AggregationStrategy aggregationStrategy,
                          ExecutorService executorService,
                          boolean shutdownExecutorService)
Method Detail

toString

public String toString()
Overrides:
toString in class Object

getTraceLabel

public String getTraceLabel()
Description copied from interface: Traceable
Gets the trace label used for logging when tracing is enabled.

The label should be short and precise.

Specified by:
getTraceLabel in interface Traceable
Returns:
the label

next

public List<Processor> next()
Description copied from interface: Navigate
Next group of outputs

Specified by:
next in interface Navigate<Processor>
Returns:
next group or null if no more outputs

hasNext

public boolean hasNext()
Description copied from interface: Navigate
Are there more outputs?

Specified by:
hasNext in interface Navigate<Processor>
Returns:
true if more outputs

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.

isCompleted

protected String isCompleted(String key,
                             Exchange exchange)
Tests whether the given exchange is complete or not

Parameters:
key - the correlation key
exchange - the incoming exchange
Returns:
null if not completed, otherwise a String with the type that triggered the completion

onAggregation

protected Exchange onAggregation(Exchange oldExchange,
                                 Exchange newExchange)

onCompletion

protected void onCompletion(String key,
                            Exchange exchange,
                            boolean fromTimeout)

restoreTimeoutMapFromAggregationRepository

protected void restoreTimeoutMapFromAggregationRepository()
                                                   throws Exception
Restores the timeout map with timeout values from the aggregation repository.

This is needed in case the aggregator has been stopped and started again (for example a server restart). Then the existing exchanges from the AggregationRepository must have its timeout conditions restored.

Throws:
Exception

getCompletionPredicate

public Predicate getCompletionPredicate()

setCompletionPredicate

public void setCompletionPredicate(Predicate completionPredicate)

isEagerCheckCompletion

public boolean isEagerCheckCompletion()

setEagerCheckCompletion

public void setEagerCheckCompletion(boolean eagerCheckCompletion)

getCompletionTimeout

public long getCompletionTimeout()

setCompletionTimeout

public void setCompletionTimeout(long completionTimeout)

getCompletionTimeoutExpression

public Expression getCompletionTimeoutExpression()

setCompletionTimeoutExpression

public void setCompletionTimeoutExpression(Expression completionTimeoutExpression)

getCompletionInterval

public long getCompletionInterval()

setCompletionInterval

public void setCompletionInterval(long completionInterval)

getCompletionSize

public int getCompletionSize()

setCompletionSize

public void setCompletionSize(int completionSize)

getCompletionSizeExpression

public Expression getCompletionSizeExpression()

setCompletionSizeExpression

public void setCompletionSizeExpression(Expression completionSizeExpression)

isIgnoreInvalidCorrelationKeys

public boolean isIgnoreInvalidCorrelationKeys()

setIgnoreInvalidCorrelationKeys

public void setIgnoreInvalidCorrelationKeys(boolean ignoreInvalidCorrelationKeys)

getCloseCorrelationKeyOnCompletion

public Integer getCloseCorrelationKeyOnCompletion()

setCloseCorrelationKeyOnCompletion

public void setCloseCorrelationKeyOnCompletion(Integer closeCorrelationKeyOnCompletion)

isCompletionFromBatchConsumer

public boolean isCompletionFromBatchConsumer()

setCompletionFromBatchConsumer

public void setCompletionFromBatchConsumer(boolean completionFromBatchConsumer)

getExceptionHandler

public ExceptionHandler getExceptionHandler()

setExceptionHandler

public void setExceptionHandler(ExceptionHandler exceptionHandler)

isParallelProcessing

public boolean isParallelProcessing()

setParallelProcessing

public void setParallelProcessing(boolean parallelProcessing)

getAggregationRepository

public AggregationRepository getAggregationRepository()

setAggregationRepository

public void setAggregationRepository(AggregationRepository aggregationRepository)

isDiscardOnCompletionTimeout

public boolean isDiscardOnCompletionTimeout()

setDiscardOnCompletionTimeout

public void setDiscardOnCompletionTimeout(boolean discardOnCompletionTimeout)

setForceCompletionOnStop

public void setForceCompletionOnStop(boolean forceCompletionOnStop)

setTimeoutCheckerExecutorService

public void setTimeoutCheckerExecutorService(ScheduledExecutorService timeoutCheckerExecutorService)

getTimeoutCheckerExecutorService

public ScheduledExecutorService getTimeoutCheckerExecutorService()

isShutdownTimeoutCheckerExecutorService

public boolean isShutdownTimeoutCheckerExecutorService()

setShutdownTimeoutCheckerExecutorService

public void setShutdownTimeoutCheckerExecutorService(boolean shutdownTimeoutCheckerExecutorService)

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()

prepareShutdown

public void prepareShutdown(boolean forced)
Description copied from interface: ShutdownPrepared
Prepares for shutdown.

The ShutdownStrategy supports preparing for shutdown using two steps. First a regular preparation, where the given forced parameter will be false. And if the shutdown times out, then the ShutdownStrategy performs a more aggressive shutdown, calling this method a second time with true for the given forced parameter.

For example by graceful stopping any threads or the likes.

For forced shutdown, then the service is expected to aggressively shutdown any child services, such as thread pools etc. This is the last chance it has to perform such duties.

Specified by:
prepareShutdown in interface ShutdownPrepared
Parameters:
forced - true is forcing a more aggressive shutdown, false is for preparing to shutdown.

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

forceCompletionOfAllGroups

public int forceCompletionOfAllGroups()


Apache CAMEL