org.apache.camel.processor
Class StreamResequencer

java.lang.Object
  extended by org.apache.camel.impl.ServiceSupport
      extended by org.apache.camel.processor.StreamResequencer
All Implemented Interfaces:
Navigate<Processor>, Processor, SequenceSender<Exchange>, Traceable, Service

public class StreamResequencer
extends ServiceSupport
implements SequenceSender<Exchange>, Processor, Navigate<Processor>, Traceable

A resequencer that re-orders a (continuous) stream of Exchanges. The algorithm implemented by ResequencerEngine is based on the detection of gaps in a message stream rather than on a fixed batch size. Gap detection in combination with timeouts removes the constraint of having to know the number of messages of a sequence (i.e. the batch size) in advance.

Messages must contain a unique sequence number for which a predecessor and a successor is known. For example a message with the sequence number 3 has a predecessor message with the sequence number 2 and a successor message with the sequence number 4. The message sequence 2,3,5 has a gap because the sucessor of 3 is missing. The resequencer therefore has to retain message 5 until message 4 arrives (or a timeout occurs).

Instances of this class poll for Exchanges from a given endpoint. Resequencing work and the delivery of messages to the next processor is done within the single polling thread.

Version:
$Revision: 792319 $
See Also:
ResequencerEngine

Constructor Summary
StreamResequencer(Processor processor, SequenceElementComparator<Exchange> comparator)
          Creates a new StreamResequencer instance.
 
Method Summary
protected  void doStart()
           
protected  void doStop()
           
 int getCapacity()
          Returns this resequencer's capacity.
 ExceptionHandler getExceptionHandler()
          Returns this resequencer's exception handler.
 Processor getProcessor()
          Returns the next processor.
 long getTimeout()
          Returns this resequencer's timeout.
 String getTraceLabel()
          Gets the trace label used for logging when tracing is enabled.
 boolean hasNext()
          Are there more outputs?
 List<Processor> next()
          Next group of outputs
 void process(Exchange exchange)
          Processes the message exchange
 void sendElement(Exchange exchange)
          Sends the exchange to the next processor.
 void setCapacity(int capacity)
           
 void setTimeout(long timeout)
           
 String toString()
           
 
Methods inherited from class org.apache.camel.impl.ServiceSupport
addChildService, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, removeChildService, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StreamResequencer

public StreamResequencer(Processor processor,
                         SequenceElementComparator<Exchange> comparator)
Creates a new StreamResequencer instance.

Parameters:
processor - next processor that processes re-ordered exchanges.
comparator - a sequence element comparator for exchanges.
Method Detail

getExceptionHandler

public ExceptionHandler getExceptionHandler()
Returns this resequencer's exception handler.


getProcessor

public Processor getProcessor()
Returns the next processor.


getCapacity

public int getCapacity()
Returns this resequencer's capacity. The capacity is the maximum number of exchanges that can be managed by this resequencer at a given point in time. If the capacity if reached, polling from the endpoint will be skipped for timeout milliseconds giving exchanges the possibility to time out and to be delivered after the waiting period.

Returns:
this resequencer's capacity.

getTimeout

public long getTimeout()
Returns this resequencer's timeout. This sets the resequencer engine's timeout via ResequencerEngine.setTimeout(long). This value is also used to define the polling timeout from the endpoint.

Returns:
this resequencer's timeout. (Processor)
See Also:
ResequencerEngine.setTimeout(long)

setCapacity

public void setCapacity(int capacity)

setTimeout

public void setTimeout(long timeout)

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 lable should be short and precise.

Specified by:
getTraceLabel in interface Traceable
Returns:
the label

doStart

protected void doStart()
                throws Exception
Specified by:
doStart in class ServiceSupport
Throws:
Exception

doStop

protected void doStop()
               throws Exception
Specified by:
doStop in class ServiceSupport
Throws:
Exception

sendElement

public void sendElement(Exchange exchange)
                 throws Exception
Sends the exchange to the next processor.

Specified by:
sendElement in interface SequenceSender<Exchange>
Parameters:
exchange - exchange to send.
Throws:
Exception - if delivery fails.

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.

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

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


Apache CAMEL