Package org.pipecraft.pipes.terminal
Class QueueWriterPipe<T>
- java.lang.Object
-
- org.pipecraft.pipes.terminal.TerminalPipe
-
- org.pipecraft.pipes.terminal.QueueWriterPipe<T>
-
- Type Parameters:
T- the queue item data type
- All Implemented Interfaces:
Closeable,AutoCloseable,BasePipe
public class QueueWriterPipe<T> extends TerminalPipe
A terminal pipe writing items to aBlockingQueue. Uses special item values as queue end marker indicators. For proper behavior, the same marker references (one for error and one for successful termination) should also be used by the queue consumer. Note that this pipe can cause a deadlock if not used properly. Calling start() will start pushing to the queue in a blocking manner, meaning that the consumer should be a different thread (or alternatively the queue should not be bounded, which is memory risky).- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description QueueWriterPipe(Pipe<T> inputPipe, BlockingQueue<T> queue, T successMarker, T errorMarker)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()floatgetProgress()voidstart()Performs pre-processing prior to item flow throw the pipe.
-
-
-
Constructor Detail
-
QueueWriterPipe
public QueueWriterPipe(Pipe<T> inputPipe, BlockingQueue<T> queue, T successMarker, T errorMarker)
Constructor- Parameters:
inputPipe- The pipe to read items from and push them to the queuequeue- The queue to write tosuccessMarker- Used for indicating data completion with success. Should be a unique reference reserved for this purpose. Consumer should use the same marker reference for end of input indication.errorMarker- Used for indicating an error termination. Should be a unique reference reserved for this purpose. Consumer should use the same marker reference for error indication.
-
-
Method Detail
-
start
public void start() throws PipeException, InterruptedExceptionDescription copied from interface:BasePipePerforms pre-processing prior to item flow throw the pipe. Implementations must call the same method for all their input pipes before accessing their items. This is typically done here.- Throws:
PipeException- In case of pipe errors in this pipe or somewhere up-stream.InterruptedException- In case that the operation has been interrupted by another thread.
-
getProgress
public float getProgress()
- Specified by:
getProgressin interfaceBasePipe- Overrides:
getProgressin classTerminalPipe- Returns:
- The pipe flow progress, as a floating number between 0.0 and 1.0. Important implementation rules: 1) Calling this method before start() call is complete isn't allowed and has an undefined behavior. 2) Implementation should do best effort to provide an estimate of the progress this pipe has made (0.0 - 1.0) 3) When the pipe is fully consumed, getProgress() should return 1.0. 4) Results must be monotonous, i.e. results of consecutive calls may never be decreasing. 5) Thread safety: progress may be maintained by some thread/s but monitoring by other threads. Implementations must be thread safe.
-
close
public void close() throws IOException- Throws:
IOException
-
-