gr.uoa.di.madgik.grs.reader.decorators.keepalive
Class KeepAliveReader<T extends Record>

java.lang.Object
  extended by gr.uoa.di.madgik.grs.reader.decorators.RecordReaderDelegate<T>
      extended by gr.uoa.di.madgik.grs.reader.decorators.keepalive.KeepAliveReader<T>
Type Parameters:
T - The type of Record specialization that is to be returned by the get operations
All Implemented Interfaces:
IRecordReader<T>, java.lang.Iterable<T>

public class KeepAliveReader<T extends Record>
extends RecordReaderDelegate<T>

A KeepAliveReader adds keep-alive functionality to a IRecordReader. More specifically, it reads records from the underlying reader periodically, based on a time interval. This functionality is transparent to the client, as each time a get method is called, the record either originates from the set of prefetched records, or it is actually read directly from the underlying reader. Keep-alive functionality is useful in order to keep the communication channel open if a client wishes to delay the consumption of records for long periods of time and the producer has no indication that this behavior is not problematic.

Author:
gerasimos.farantatos

Constructor Summary
KeepAliveReader(IRecordReader<T> reader, long keepAliveFrequency, java.util.concurrent.TimeUnit keepAliveFrequencyUnit)
          Creates a new instance
KeepAliveReader(IRecordReader<T> reader, long keepAliveFrequency, java.util.concurrent.TimeUnit keepAliveFrequencyUnit, long inactivityTimeout, java.util.concurrent.TimeUnit inactivityTimeUnit)
           
 
Method Summary
 long currentRecord()
          The index of the last Record retrieved through the reader.
 T get()
          Retrieves the next available record, which will be either one of the records retrieved via the keep-alive operation or a record retrieved directly from the underlying IRecordReader
 T get(long timeout, java.util.concurrent.TimeUnit unit)
          Retrieves the next available record, which will be either one of the records retrieved via the keep-alive operation or a record retrieved directly from the underlying IRecordReader
 long seek(long len)
          Seeks the number of Records provided in the list of Records available.
 
Methods inherited from class gr.uoa.di.madgik.grs.reader.decorators.RecordReaderDelegate
availableRecords, close, emit, getCapacity, getConcurrentPartialCapacity, getInactivityTimeout, getInactivityTimeUnit, getIteratorTimeout, getIteratorTimeUnit, getRecordDefinitions, getStatus, iterator, receive, setIteratorTimeout, setIteratorTimeUnit, totalRecords, waitAvailable
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeepAliveReader

public KeepAliveReader(IRecordReader<T> reader,
                       long keepAliveFrequency,
                       java.util.concurrent.TimeUnit keepAliveFrequencyUnit)
                throws GRS2ReaderInvalidArgumentException
Creates a new instance

Parameters:
reader - the IRecordReader to which keep-alive functionality is to be added
keepAliveFrequency - the frequency of the keep-alive operation
keepAliveFrequencyUnit - the time unit of the frequency of the keep-alive operation
Throws:
GRS2ReaderInvalidArgumentException - one of the arguments is not valid

KeepAliveReader

public KeepAliveReader(IRecordReader<T> reader,
                       long keepAliveFrequency,
                       java.util.concurrent.TimeUnit keepAliveFrequencyUnit,
                       long inactivityTimeout,
                       java.util.concurrent.TimeUnit inactivityTimeUnit)
                throws GRS2ReaderInvalidArgumentException
Parameters:
reader - the IRecordReader to which keep-alive functionality is to be added
keepAliveFrequency - the frequency of the keep-alive operation
keepAliveFrequencyUnit - the time unit of the frequency of the keep-alive operation
inactivityTimeout - A timeout after which the keep-alive reader will close the underlying reader
inactivityTimeUnit - The time unit of the inactivity timeout
Throws:
GRS2ReaderInvalidArgumentException - one of the arguments is not valid
Method Detail

get

public T get()
                     throws GRS2ReaderException
Retrieves the next available record, which will be either one of the records retrieved via the keep-alive operation or a record retrieved directly from the underlying IRecordReader

Retrieves the next available Record from the IBuffer. An explicit cast is performed to the type provided in the generic declaration of the reader.

Specified by:
get in interface IRecordReader<T extends Record>
Overrides:
get in class RecordReaderDelegate<T extends Record>
Returns:
the retrieved Record or null if none was readily available
Throws:
GRS2ReaderException - the operation could not be completed
See Also:
IRecordReader#get()}

get

public T get(long timeout,
             java.util.concurrent.TimeUnit unit)
                     throws GRS2ReaderException
Retrieves the next available record, which will be either one of the records retrieved via the keep-alive operation or a record retrieved directly from the underlying IRecordReader

Retrieves the next available Record from the IBuffer. An explicit cast is performed to the type provided in the generic declaration of the reader. If no record is readily available the reader will block for a maximum of timeout unit units of time waiting for a record to become available or until the IBuffer uses the IBuffer.getReaderImmediateNotificationObject() to notify blocked readers.

Specified by:
get in interface IRecordReader<T extends Record>
Overrides:
get in class RecordReaderDelegate<T extends Record>
Parameters:
timeout - the timeout to wait for
unit - the unit of time to use to interpret the timeout value
Returns:
the Record retrieved, or null if the timeout expired without a Record becoming available
Throws:
GRS2ReaderException - the operation could not be completed
See Also:
IRecordReader#get(long, TimeUnit)}

seek

public long seek(long len)
          throws GRS2ReaderException
Seeks the number of Records provided in the list of Records available. Depending on the actual IRecordReader implementation, forward and/or backward seeks may be supported.

Specified by:
seek in interface IRecordReader<T extends Record>
Overrides:
seek in class RecordReaderDelegate<T extends Record>
Parameters:
len - the number of Record}s to skip
Returns:
the number of Records actually skipped
Throws:
GRS2ReaderException - the operation could not be completed
See Also:
IRecordReader#seek(long)}

currentRecord

public long currentRecord()
                   throws GRS2ReaderException
The index of the last Record retrieved through the reader.

Specified by:
currentRecord in interface IRecordReader<T extends Record>
Overrides:
currentRecord in class RecordReaderDelegate<T extends Record>
Returns:
the index of the last Record retrieved through the reader
Throws:
GRS2ReaderException - the operation could not be completed because no records were retrieved yet
See Also:
IRecordReader#currentRecord()}