Class ResequencerEngine<E>
SequenceElementComparator. 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 a SequenceSender.
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 the SequenceSender. The lower the timeout 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 the deliver()
or deliverNext() 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 needs timeout milliseconds in any case for becoming ready-for-delivery.
-
Constructor Summary
ConstructorsConstructorDescriptionResequencerEngine(SequenceElementComparator<E> comparator) Creates a new resequencer instance with a default timeout of 2000 milliseconds. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeliver()Delivers all elements which are currently ready to deliver.booleanAttempts to deliver a single element from the head of the resequencer queue (sequence).Returns the sequence sender.longReturns this resequencer's timeout value.voidInserts the given element into this resequencer.voidsetRejectOld(Boolean rejectOld) voidsetSequenceSender(SequenceSender<E> sequenceSender) Sets the sequence sender.voidsetTimeout(long timeout) Sets this sequencer's timeout value.intsize()Returns the number of elements currently maintained by this resequencer.voidstart()voidstop()Stops this resequencer (i.e. this resequencer'sTimerinstance).voidWait for the following condition to happen.
-
Constructor Details
-
ResequencerEngine
Creates a new resequencer instance with a default timeout of 2000 milliseconds.- Parameters:
comparator- a sequence element comparator.
-
-
Method Details
-
start
public void start() -
stop
public void stop()Stops this resequencer (i.e. this resequencer'sTimerinstance). -
size
public int size()Returns the number of elements currently maintained by this resequencer.- Returns:
- the number of elements currently maintained by this resequencer.
-
waitUntil
Wait for the following condition to happen. Do not call this method while holding a lock on the resequencer engine, as it will deadlock. The predicate will be evaluated while holding a lock on the resequencer engine.- Parameters:
pred- the condition to wait for- Throws:
InterruptedException- if the thread is interrupted
-
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
-
setRejectOld
-
getSequenceSender
Returns the sequence sender.- Returns:
- the sequence sender.
-
setSequenceSender
Sets the sequence sender.- Parameters:
sequenceSender- a sequence element sender.
-
insert
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
Delivers all elements which are currently ready to deliver.- Throws:
Exception- thrown bySequenceSender.sendElement(Object).- See Also:
-
deliverNext
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:
trueif the element has been deliveredfalseotherwise.- Throws:
Exception- thrown bySequenceSender.sendElement(Object).
-