org.apache.camel.processor
Class StreamResequencer

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

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 successor 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:
See Also:
ResequencerEngine

Field Summary
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
StreamResequencer(CamelContext camelContext, Processor processor, SequenceElementComparator<Exchange> comparator)
          Creates a new StreamResequencer instance.
 
Method Summary
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 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?
 boolean isIgnoreInvalidExchanges()
           
 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 setIgnoreInvalidExchanges(boolean ignoreInvalidExchanges)
          Sets whether to ignore invalid exchanges which cannot be used by this stream resequencer.
 void setTimeout(long timeout)
           
 String toString()
           
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doShutdown, 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
 

Constructor Detail

StreamResequencer

public StreamResequencer(CamelContext camelContext,
                         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)

isIgnoreInvalidExchanges

public boolean isIgnoreInvalidExchanges()

setIgnoreInvalidExchanges

public void setIgnoreInvalidExchanges(boolean ignoreInvalidExchanges)
Sets whether to ignore invalid exchanges which cannot be used by this stream resequencer.

Default is false, by which an CamelExchangeException is thrown if the Exchange is invalid.


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

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

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