com.duowan.mobile.netroid
Class RequestQueue

java.lang.Object
  extended by com.duowan.mobile.netroid.RequestQueue

public class RequestQueue
extends java.lang.Object

A request dispatch queue with a thread pool of dispatchers. Calling add(Request) will enqueue the given Request for dispatch, resolving from either cache or network on a worker thread, and then delivering a parsed response on the main thread.


Nested Class Summary
static interface RequestQueue.RequestFilter
          A simple predicate or filter interface for Requests, for use by cancelAll(RequestFilter).
 
Field Summary
static int DEFAULT_NETWORK_THREAD_POOL_SIZE
          Number of network request dispatcher threads to start.
 
Constructor Summary
RequestQueue(Network network)
          Creates the worker pool.
RequestQueue(Network network, int threadPoolSize, Delivery delivery, DiskCache cache)
          Creates the worker pool.
RequestQueue(Network network, int threadPoolSize, DiskCache cache)
          Creates the worker pool.
 
Method Summary
 Request add(Request request)
          Adds a Request to the dispatch queue.
 void cancelAll(java.lang.Object tag)
          Cancels all requests in this queue with the given tag.
 void cancelAll(RequestQueue.RequestFilter filter)
          Cancels all requests in this queue for which the given filter applies.
(package private)  void finish(Request request)
          Called from Request.finish(String), indicating that processing of the given request has finished.
 int getSequenceNumber()
          Gets a sequence number.
 int getThreadPoolSize()
          Gets the thread pool size.
 void start()
          Starts the dispatchers in this queue.
 void stop()
          Stops the cache and network dispatchers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NETWORK_THREAD_POOL_SIZE

public static final int DEFAULT_NETWORK_THREAD_POOL_SIZE
Number of network request dispatcher threads to start.

See Also:
Constant Field Values
Constructor Detail

RequestQueue

public RequestQueue(Network network,
                    int threadPoolSize,
                    Delivery delivery,
                    DiskCache cache)
Creates the worker pool. Processing will not begin until start() is called.

Parameters:
network - A Network interface for performing HTTP requests
threadPoolSize - Number of network dispatcher threads to create
delivery - A Delivery interface for posting responses and errors
cache - A Cache to use for persisting responses to disk

RequestQueue

public RequestQueue(Network network,
                    int threadPoolSize,
                    DiskCache cache)
Creates the worker pool. Processing will not begin until start() is called.

Parameters:
network - A Network interface for performing HTTP requests
threadPoolSize - Number of network dispatcher threads to create
cache - A Cache to use for persisting responses to disk

RequestQueue

public RequestQueue(Network network)
Creates the worker pool. Processing will not begin until start() is called.

Parameters:
network - A Network interface for performing HTTP requests
Method Detail

start

public void start()
Starts the dispatchers in this queue.


stop

public void stop()
Stops the cache and network dispatchers.


getSequenceNumber

public int getSequenceNumber()
Gets a sequence number.


getThreadPoolSize

public int getThreadPoolSize()
Gets the thread pool size.


cancelAll

public void cancelAll(RequestQueue.RequestFilter filter)
Cancels all requests in this queue for which the given filter applies.

Parameters:
filter - The filtering function to use

cancelAll

public void cancelAll(java.lang.Object tag)
Cancels all requests in this queue with the given tag. Tag must be non-null and equality is by identity.


add

public Request add(Request request)
Adds a Request to the dispatch queue.

Parameters:
request - The request to service
Returns:
The passed-in request

finish

void finish(Request request)
Called from Request.finish(String), indicating that processing of the given request has finished.

Releases waiting requests for request.getCacheKey() if request.shouldCache().