com.vodafone360.people.service.utils
Class TimeOutWatcher

java.lang.Object
  extended by com.vodafone360.people.service.utils.TimeOutWatcher
All Implemented Interfaces:
java.lang.Runnable

public class TimeOutWatcher
extends java.lang.Object
implements java.lang.Runnable

TimeOutWatcher utility... This is a utility class that is intended to dispatch time-outs for each sent request individually. The thread should be managing only the requests which can expire, i.e. don't add request with timeout "-1" TODO: Consider using the WorkerThread to check for timeouts, that would save us one thread! !!! Also ResponseQueue, RequestQueue and TimeOutWatcher should be managed together and used via a common mutex !!!


Field Summary
private  boolean mIsRunning
          Flag to determine whether or not the watcher is running.
private  java.util.LinkedList<Request> mRequests
          The list of watched requests sorted by their expiry dates in ascending order.
private  java.lang.Thread mThread
          The thread watching for timed out requests.
 
Constructor Summary
TimeOutWatcher()
          Constructor.
 
Method Summary
 void addRequest(Request request)
          Adds a request to be watched for timeouts.
private  void fireRequestExpired(Request request)
          Creates a TimeOut event and adds it to the response queue.
private  long getNextRuntime()
          Finds the closest time to perform a new check on timeouts.
 Request[] getRequestsArray()
          Gets an array containing all the requests being watched for timeouts.
 int getRequestsCount()
          Gets the current number of requests being watched for timeouts.
private  int insertRequestByExpiryDate(Request request)
          Inserts a request in the requests list while maintaining it sorted by ascending order of expiry date.
 void invalidateAllRequests()
          Sends a timeout event for all the requests.
 void kill()
          Kills the TimeOutWatcher (releases memory and running thread).
 void removeRequest(Request request)
          Removes a request from being watched for timeouts.
 void run()
          TimeOutWatcher thread that checks for the next time to run, sleeps when not busy and sends timeouts.
private  void sendTimeoutEvent(long currentTime)
          Sends a timeout event for all the expired requests.
private  void startThread()
          Starts the TimeOutWatcher thread and performs initialization.
private  void stopThread()
          Stops the TimeOutWatcher thread and releases the memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mIsRunning

private boolean mIsRunning
Flag to determine whether or not the watcher is running.


mRequests

private java.util.LinkedList<Request> mRequests
The list of watched requests sorted by their expiry dates in ascending order.


mThread

private java.lang.Thread mThread
The thread watching for timed out requests.

Constructor Detail

TimeOutWatcher

public TimeOutWatcher()
Constructor.

Method Detail

run

public void run()
TimeOutWatcher thread that checks for the next time to run, sleeps when not busy and sends timeouts.

Specified by:
run in interface java.lang.Runnable

startThread

private void startThread()
Starts the TimeOutWatcher thread and performs initialization. Note: this method shall be called within synchronized(this) block.


stopThread

private void stopThread()
Stops the TimeOutWatcher thread and releases the memory. Note: this method shall be called within synchronized(this) block.


getNextRuntime

private long getNextRuntime()
Finds the closest time to perform a new check on timeouts.

Returns:
the next time when a timeout check is needed, -1 if no nothing to perform

sendTimeoutEvent

private void sendTimeoutEvent(long currentTime)
Sends a timeout event for all the expired requests.

Parameters:
currentTime - the current time until when a timeout event needs to be sent

insertRequestByExpiryDate

private int insertRequestByExpiryDate(Request request)
Inserts a request in the requests list while maintaining it sorted by ascending order of expiry date.

Parameters:
request - the request to insert

fireRequestExpired

private void fireRequestExpired(Request request)
Creates a TimeOut event and adds it to the response queue. FIXME: this assumes that adding a request to the response queue will trigger a synchronous removeRequest() call with the same thread.

Parameters:
request - the request that has timed out

addRequest

public void addRequest(Request request)
Adds a request to be watched for timeouts. Note: it is assumed that the request expiry date is calculated.

Parameters:
request - the request to add

removeRequest

public void removeRequest(Request request)
Removes a request from being watched for timeouts.

Parameters:
request - the request to remove

kill

public void kill()
Kills the TimeOutWatcher (releases memory and running thread).


invalidateAllRequests

public void invalidateAllRequests()
Sends a timeout event for all the requests.


getRequestsCount

public int getRequestsCount()
Gets the current number of requests being watched for timeouts.


getRequestsArray

public Request[] getRequestsArray()
Gets an array containing all the requests being watched for timeouts.

Returns:
array containing all requests, NULL if list of requests maintained internally is NLL or empty.


JavaDoc