com.vodafone360.people.service.io
Class RequestQueue

java.lang.Object
  extended by com.vodafone360.people.service.io.RequestQueue

public class RequestQueue
extends java.lang.Object

Holds a queue of outgoing requests. The Requester adds Requests to the queue. The transport layer gets one or more items from the queue when it is ready to send more requests to the server. When a Request is added a request id is generated for this request. Requests are removed from the queue on completion or if an error requires us to clear any outstanding requests.


Nested Class Summary
private static class RequestQueue.RequestQueueHolder
          Use Initialization on demand holder pattern
 
Field Summary
private  int mCurrentRequestId
          A unique ID identifying this request
private static int MILLIS_PER_SECOND
           
private  java.util.List<IQueueListener> mListeners
          Contains a list of listeners that will receive events when items are added to the queue.
private  java.util.List<Request> mRequests
          The queue data, a List-array of Request items.
private  TimeOutWatcher mTimeOutWatcher
           
 
Constructor Summary
protected RequestQueue()
          Constructs the request queue
 
Method Summary
protected  void addQueueListener(IQueueListener listener)
          Add listener listening for RequestQueue changes.
protected  int addRequest(Request req)
          Adds a request to the queue without sending an event to the listeners
protected  int[] addRequest(Request[] requests)
          Adds requests to the queue.
protected  int addRequestAndNotify(Request req)
          Add request to queue
protected  void clearActiveRequests(boolean rpgOnly)
          Clear active requests (i.e add dummy response to response queue).
protected  void clearAllRequests()
          Clears all requests from the queue and puts null responses on the response queue to tell the engines that they have been cleared.
protected  void clearTheTimeOuts()
          Removes all items that are being watched for timeouts
protected  void fireQueueStateChanged()
          Fire RequestQueue state changed message
protected  java.util.List<Request> getAllRequests()
          Returns all requests from the queue.
protected  java.util.List<Request> getApiRequests()
          Returns all requests from the queue needing the API or both to work.
protected static RequestQueue getInstance()
          Get instance of RequestQueue - we only have a single instance.
protected  Request getRequest(int requestId)
          Return Request from specified request ID.
private  java.util.List<Request> getRequests(boolean needsRpgForRequest)
          Returns a list of either requests needing user authentication or requests not needing user authentication depending on the flag passed to this method.
protected  java.util.List<Request> getRpgRequests()
          Returns all requests from the queue needing the RPG or both to work.
protected  TimeOutWatcher getTimeoutWatcher()
          Return handle to TimeOutWatcher.
protected  void removeQueueListener(IQueueListener listener)
          Remove RequestQueue listener
protected  boolean removeRequest(int requestId)
          Removes the request for the given request ID from the queue and searches the queue for requests older than Settings.REMOVE_REQUEST_FROM_QUEUE_MILLIS and removes them as well.
private  int requestCount()
           
 java.lang.String toString()
          Overrides the toString() method of Object and gives detailed infos which objects are on the queue and whether they are active or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MILLIS_PER_SECOND

private static final int MILLIS_PER_SECOND
See Also:
Constant Field Values

mRequests

private final java.util.List<Request> mRequests
The queue data, a List-array of Request items.


mCurrentRequestId

private volatile int mCurrentRequestId
A unique ID identifying this request


mListeners

private final java.util.List<IQueueListener> mListeners
Contains a list of listeners that will receive events when items are added to the queue.


mTimeOutWatcher

private TimeOutWatcher mTimeOutWatcher
Constructor Detail

RequestQueue

protected RequestQueue()
Constructs the request queue

Method Detail

getInstance

protected static RequestQueue getInstance()
Get instance of RequestQueue - we only have a single instance. If the instance of RequestQueue does not yet exist it is created.

Returns:
Instance of RequestQueue.

addQueueListener

protected void addQueueListener(IQueueListener listener)
Add listener listening for RequestQueue changes. Events are sent when items are added to the queue (or in the case of batching when the last item is added to the queue).

Parameters:
listener - listener to add

removeQueueListener

protected void removeQueueListener(IQueueListener listener)
Remove RequestQueue listener

Parameters:
listener - listener to remove

fireQueueStateChanged

protected void fireQueueStateChanged()
Fire RequestQueue state changed message


addRequestAndNotify

protected int addRequestAndNotify(Request req)
Add request to queue

Parameters:
req - Request to add to queue
Returns:
request id of new request

addRequest

protected int addRequest(Request req)
Adds a request to the queue without sending an event to the listeners

Parameters:
req - The request to add
Returns:
The unique request ID TODO: What is with the method naming convention?

addRequest

protected int[] addRequest(Request[] requests)
Adds requests to the queue.

Parameters:
requests - The requests to add.
Returns:
The request IDs generated in an integer array or null if the requests array was null. Returns NULL id requests[] is NULL.

requestCount

private int requestCount()

getAllRequests

protected java.util.List<Request> getAllRequests()
Returns all requests from the queue. Regardless if they need to

Returns:
List of all requests.

getApiRequests

protected java.util.List<Request> getApiRequests()
Returns all requests from the queue needing the API or both to work.

Returns:
List of all requests needing the API or both (API or RPG) to function properly.

getRpgRequests

protected java.util.List<Request> getRpgRequests()
Returns all requests from the queue needing the RPG or both to work.

Returns:
List of all requests needing the RPG or both (API or RPG) to function properly.

getRequests

private java.util.List<Request> getRequests(boolean needsRpgForRequest)
Returns a list of either requests needing user authentication or requests not needing user authentication depending on the flag passed to this method.

Parameters:
needsUserAuthentication - If true only requests that need to have a valid user authentication will be returned. Otherwise methods requiring application authentication will be returned.
Returns:
A list of requests with the need for application authentication or user authentication.

getRequest

protected Request getRequest(int requestId)
Return Request from specified request ID. Only used for unit tests.

Parameters:
requestId - Request Id of required request
Returns:
Request with or null if request does not exist

removeRequest

protected boolean removeRequest(int requestId)
Removes the request for the given request ID from the queue and searches the queue for requests older than Settings.REMOVE_REQUEST_FROM_QUEUE_MILLIS and removes them as well.

Parameters:
requestId - The ID of the request to remove.
Returns:
True if the request was found and removed.

clearActiveRequests

protected void clearActiveRequests(boolean rpgOnly)
Clear active requests (i.e add dummy response to response queue).

Parameters:
rpgOnly -

clearAllRequests

protected void clearAllRequests()
Clears all requests from the queue and puts null responses on the response queue to tell the engines that they have been cleared. This should be called from the connection thread as soon as it is stopped.


getTimeoutWatcher

protected TimeOutWatcher getTimeoutWatcher()
Return handle to TimeOutWatcher.

Returns:
handle to TimeOutWatcher.

clearTheTimeOuts

protected void clearTheTimeOuts()
Removes all items that are being watched for timeouts


toString

public java.lang.String toString()
Overrides the toString() method of Object and gives detailed infos which objects are on the queue and whether they are active or not.

Overrides:
toString in class java.lang.Object


JavaDoc