Class ResequencerEngine<E>
- java.lang.Object
-
- org.apache.camel.processor.resequencer.ResequencerEngine<E>
-
public class ResequencerEngine<E> extends Object
Resequences elements based on a givenSequenceElementComparator
. This resequencer is designed for resequencing element streams. Stream-based resequencing has the advantage that the number of elements to be resequenced need not be known in advance. Resequenced elements are delivered via aSequenceSender
.The resequencer's behaviour for a given comparator is controlled by the
timeout
property. This is the timeout (in milliseconds) for a given element managed by this resequencer. An out-of-sequence element can only be marked as ready-for-delivery if it either times out or if it has an immediate predecessor (in that case it is in-sequence). If an immediate predecessor of a waiting element arrives the timeout task for the waiting element will be cancelled (which marks it as ready-for-delivery).If the maximum out-of-sequence time difference between elements within a stream is known, the
timeout
value should be set to this value. In this case it is guaranteed that all elements of a stream will be delivered in sequence via theSequenceSender
. The lower thetimeout
value is compared to the out-of-sequence time difference between elements within a stream the higher the probability is for out-of-sequence elements delivered by this resequencer. Delivery of elements must be explicitly triggered by applications using thedeliver()
ordeliverNext()
methods. Only elements that are ready-for-delivery are delivered by these methods. The longer an application waits to trigger a delivery the more elements may become ready-for-delivery.The resequencer remembers the last-delivered element. If an element arrives which is the immediate successor of the last-delivered element it is ready-for-delivery immediately. After delivery the last-delivered element is adjusted accordingly. If the last-delivered element is
null
i.e. the resequencer was newly created the first arriving element needstimeout
milliseconds in any case for becoming ready-for-delivery.
-
-
Constructor Summary
Constructors Constructor Description ResequencerEngine(SequenceElementComparator<E> comparator)
Creates a new resequencer instance with a default timeout of 2000 milliseconds.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
deliver()
Delivers all elements which are currently ready to deliver.boolean
deliverNext()
Attempts to deliver a single element from the head of the resequencer queue (sequence).Boolean
getRejectOld()
SequenceSender<E>
getSequenceSender()
Returns the sequence sender.long
getTimeout()
Returns this resequencer's timeout value.void
insert(E o)
Inserts the given element into this resequencer.void
setRejectOld(Boolean rejectOld)
void
setSequenceSender(SequenceSender<E> sequenceSender)
Sets the sequence sender.void
setTimeout(long timeout)
Sets this sequencer's timeout value.int
size()
Returns the number of elements currently maintained by this resequencer.void
start()
void
stop()
Stops this resequencer (i.e.
-
-
-
Constructor Detail
-
ResequencerEngine
public ResequencerEngine(SequenceElementComparator<E> comparator)
Creates a new resequencer instance with a default timeout of 2000 milliseconds.- Parameters:
comparator
- a sequence element comparator.
-
-
Method Detail
-
start
public void start()
-
stop
public void stop()
Stops this resequencer (i.e. this resequencer'sTimer
instance).
-
size
public int size()
Returns the number of elements currently maintained by this resequencer.- Returns:
- the number of elements currently maintained by this resequencer.
-
getTimeout
public long getTimeout()
Returns this resequencer's timeout value.- Returns:
- the timeout in milliseconds.
-
setTimeout
public void setTimeout(long timeout)
Sets this sequencer's timeout value.- Parameters:
timeout
- the timeout in milliseconds.
-
getRejectOld
public Boolean getRejectOld()
-
setRejectOld
public void setRejectOld(Boolean rejectOld)
-
getSequenceSender
public SequenceSender<E> getSequenceSender()
Returns the sequence sender.- Returns:
- the sequence sender.
-
setSequenceSender
public void setSequenceSender(SequenceSender<E> sequenceSender)
Sets the sequence sender.- Parameters:
sequenceSender
- a sequence element sender.
-
insert
public void insert(E o)
Inserts the given element into this resequencer. If the element is not ready for immediate delivery and has no immediate presecessor then it is scheduled for timing out. After being timed out it is ready for delivery.- Parameters:
o
- an element.- Throws:
IllegalArgumentException
- if the element cannot be used with this resequencer engine
-
deliver
public void deliver() throws Exception
Delivers all elements which are currently ready to deliver.- Throws:
Exception
- thrown bySequenceSender.sendElement(Object)
.- See Also:
deliverNext()
-
deliverNext
public boolean deliverNext() throws Exception
Attempts to deliver a single element from the head of the resequencer queue (sequence). Only elements which have not been scheduled for timing out or which already timed out can be delivered. Elements are delivered viaSequenceSender.sendElement(Object)
.- Returns:
true
if the element has been deliveredfalse
otherwise.- Throws:
Exception
- thrown bySequenceSender.sendElement(Object)
.
-
-