org.apache.camel.processor
Class MulticastProcessor

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.processor.MulticastProcessor
All Implemented Interfaces:
AsyncProcessor, Navigate<Processor>, Processor, Service, ShutdownableService, StatefulService, SuspendableService, Traceable
Direct Known Subclasses:
Pipeline, RecipientListProcessor, Splitter

public class MulticastProcessor
extends ServiceSupport
implements AsyncProcessor, Navigate<Processor>, Traceable

Implements the Multicast pattern to send a message exchange to a number of endpoints, each endpoint receiving a copy of the message exchange.

Version:
See Also:
Pipeline

Field Summary
protected  Processor onPrepare
           
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
MulticastProcessor(CamelContext camelContext, Collection<Processor> processors)
           
MulticastProcessor(CamelContext camelContext, Collection<Processor> processors, AggregationStrategy aggregationStrategy)
           
MulticastProcessor(CamelContext camelContext, Collection<Processor> processors, AggregationStrategy aggregationStrategy, boolean parallelProcessing, ExecutorService executorService, boolean shutdownExecutorService, boolean streaming, boolean stopOnException, long timeout, Processor onPrepare, boolean shareUnitOfWork)
           
 
Method Summary
protected  ExecutorService createAggregateExecutorService(String name)
          Strategy to create the thread pool for the aggregator background task which waits for and aggregates completed tasks when running in parallel mode.
protected  Processor createErrorHandler(RouteContext routeContext, Exchange exchange, Processor processor)
           
protected  ProcessorExchangePair createProcessorExchangePair(int index, Processor processor, Exchange exchange, RouteContext routeContext)
          Creates the ProcessorExchangePair which holds the processor and exchange to be send out.
protected  Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange)
           
protected  UnitOfWorkProcessor createUnitOfWorkProcessor(RouteContext routeContext, Processor processor, Exchange exchange)
          Strategy to create the UnitOfWorkProcessor to be used for the sub route
protected  void doAggregate(AggregationStrategy strategy, AtomicExchange result, Exchange exchange)
          Aggregate the Exchange with the current result
protected  void doDone(Exchange original, Exchange subExchange, AsyncCallback callback, boolean doneSync, boolean exhaust)
          Common work which must be done when we are done multicasting.
protected  void doProcessParallel(Exchange original, AtomicExchange result, Iterable<ProcessorExchangePair> pairs, boolean streaming, AsyncCallback callback)
           
protected  boolean doProcessSequential(Exchange original, AtomicExchange result, Iterable<ProcessorExchangePair> pairs, AsyncCallback callback)
           
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.
 AggregationStrategy getAggregationStrategy()
          Use getAggregationStrategy(org.apache.camel.Exchange) instead.
protected  AggregationStrategy getAggregationStrategy(Exchange exchange)
           
 CamelContext getCamelContext()
           
protected  Integer getExchangeIndex(Exchange exchange)
           
 Collection<Processor> getProcessors()
          Returns the producers to multicast to
 long getTimeout()
          An optional timeout in millis when using parallel processing
 String getTraceLabel()
          Gets the trace label used for logging when tracing is enabled.
 boolean hasNext()
          Are there more outputs?
 boolean isParallelProcessing()
           
 boolean isShareUnitOfWork()
           
 boolean isStopOnException()
          Should the multicast processor stop processing further exchanges in case of an exception occurred?
 boolean isStreaming()
          Is the multicast processor working in streaming mode?

In streaming mode: we use Iterable to ensure we can send messages as soon as the data becomes available for parallel processing, we start aggregating responses as they get send back to the processor; this means the AggregationStrategy has to take care of handling out-of-order arrival of exchanges

 List<Processor> next()
          Next group of outputs
protected  void prepareSharedUnitOfWork(Exchange childExchange, Exchange parentExchange)
          Prepares the exchange for participating in a shared unit of work

This ensures a child exchange can access its parent UnitOfWork when it participate in a shared unit of work.

 void process(Exchange exchange)
          Processes the message exchange
 boolean process(Exchange exchange, AsyncCallback callback)
          Processes the message exchange.
protected  void removeAggregationStrategyFromExchange(Exchange exchange)
          Removes the associated AggregationStrategy from the Exchange which must be done after use.
protected  void setAggregationStrategyOnExchange(Exchange exchange, AggregationStrategy aggregationStrategy)
          Sets the given AggregationStrategy on the Exchange.
protected static void setToEndpoint(Exchange exchange, Processor processor)
           
 String toString()
           
protected  void updateNewExchange(Exchange exchange, int index, Iterable<ProcessorExchangePair> allPairs, Iterator<ProcessorExchangePair> it)
           
 
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

onPrepare

protected final Processor onPrepare
Constructor Detail

MulticastProcessor

public MulticastProcessor(CamelContext camelContext,
                          Collection<Processor> processors)

MulticastProcessor

public MulticastProcessor(CamelContext camelContext,
                          Collection<Processor> processors,
                          AggregationStrategy aggregationStrategy)

MulticastProcessor

public MulticastProcessor(CamelContext camelContext,
                          Collection<Processor> processors,
                          AggregationStrategy aggregationStrategy,
                          boolean parallelProcessing,
                          ExecutorService executorService,
                          boolean shutdownExecutorService,
                          boolean streaming,
                          boolean stopOnException,
                          long timeout,
                          Processor onPrepare,
                          boolean shareUnitOfWork)
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

getCamelContext

public CamelContext getCamelContext()

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)

doProcessParallel

protected void doProcessParallel(Exchange original,
                                 AtomicExchange result,
                                 Iterable<ProcessorExchangePair> pairs,
                                 boolean streaming,
                                 AsyncCallback callback)
                          throws Exception
Throws:
Exception

doProcessSequential

protected boolean doProcessSequential(Exchange original,
                                      AtomicExchange result,
                                      Iterable<ProcessorExchangePair> pairs,
                                      AsyncCallback callback)
                               throws Exception
Throws:
Exception

doDone

protected void doDone(Exchange original,
                      Exchange subExchange,
                      AsyncCallback callback,
                      boolean doneSync,
                      boolean exhaust)
Common work which must be done when we are done multicasting.

This logic applies for both running synchronous and asynchronous as there are multiple exist points when using the asynchronous routing engine. And therefore we want the logic in one method instead of being scattered.

Parameters:
original - the original exchange
subExchange - the current sub exchange, can be null for the synchronous part
callback - the callback
doneSync - the doneSync parameter to call on callback
exhaust - whether or not error handling is exhausted

doAggregate

protected void doAggregate(AggregationStrategy strategy,
                           AtomicExchange result,
                           Exchange exchange)
Aggregate the Exchange with the current result

Parameters:
strategy - the aggregation strategy to use
result - the current result
exchange - the exchange to be added to the result

updateNewExchange

protected void updateNewExchange(Exchange exchange,
                                 int index,
                                 Iterable<ProcessorExchangePair> allPairs,
                                 Iterator<ProcessorExchangePair> it)

getExchangeIndex

protected Integer getExchangeIndex(Exchange exchange)

createProcessorExchangePairs

protected Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange)
                                                                throws Exception
Throws:
Exception

createProcessorExchangePair

protected ProcessorExchangePair createProcessorExchangePair(int index,
                                                            Processor processor,
                                                            Exchange exchange,
                                                            RouteContext routeContext)
Creates the ProcessorExchangePair which holds the processor and exchange to be send out.

You must use this method to create the instances of ProcessorExchangePair as they need to be specially prepared before use.

Parameters:
index - the index
processor - the processor
exchange - the exchange
routeContext - the route context
Returns:
prepared for use

createErrorHandler

protected Processor createErrorHandler(RouteContext routeContext,
                                       Exchange exchange,
                                       Processor processor)

createUnitOfWorkProcessor

protected UnitOfWorkProcessor createUnitOfWorkProcessor(RouteContext routeContext,
                                                        Processor processor,
                                                        Exchange exchange)
Strategy to create the UnitOfWorkProcessor to be used for the sub route

Parameters:
routeContext - the route context
processor - the processor wrapped in this unit of work processor
exchange - the exchange
Returns:
the unit of work processor

prepareSharedUnitOfWork

protected void prepareSharedUnitOfWork(Exchange childExchange,
                                       Exchange parentExchange)
Prepares the exchange for participating in a shared unit of work

This ensures a child exchange can access its parent UnitOfWork when it participate in a shared unit of work.

Parameters:
childExchange - the child exchange
parentExchange - the parent exchange

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

createAggregateExecutorService

protected ExecutorService createAggregateExecutorService(String name)
Strategy to create the thread pool for the aggregator background task which waits for and aggregates completed tasks when running in parallel mode.

Parameters:
name - the suggested name for the background thread
Returns:
the thread pool

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

setToEndpoint

protected static void setToEndpoint(Exchange exchange,
                                    Processor processor)

getAggregationStrategy

protected AggregationStrategy getAggregationStrategy(Exchange exchange)

setAggregationStrategyOnExchange

protected void setAggregationStrategyOnExchange(Exchange exchange,
                                                AggregationStrategy aggregationStrategy)
Sets the given AggregationStrategy on the Exchange.

Parameters:
exchange - the exchange
aggregationStrategy - the strategy

removeAggregationStrategyFromExchange

protected void removeAggregationStrategyFromExchange(Exchange exchange)
Removes the associated AggregationStrategy from the Exchange which must be done after use.

Parameters:
exchange - the current exchange

isStreaming

public boolean isStreaming()
Is the multicast processor working in streaming mode?

In streaming mode:


isStopOnException

public boolean isStopOnException()
Should the multicast processor stop processing further exchanges in case of an exception occurred?


getProcessors

public Collection<Processor> getProcessors()
Returns the producers to multicast to


getTimeout

public long getTimeout()
An optional timeout in millis when using parallel processing


getAggregationStrategy

public AggregationStrategy getAggregationStrategy()
Use getAggregationStrategy(org.apache.camel.Exchange) instead.


isParallelProcessing

public boolean isParallelProcessing()

isShareUnitOfWork

public boolean isShareUnitOfWork()

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


Apache CAMEL