com.gridsystems.innergrid.kernel.genericutils
Class PagedRequest<E>

java.lang.Object
  extended by java.lang.Thread
      extended by com.gridsystems.innergrid.kernel.genericutils.PagedRequest<E>
Type Parameters:
E - Object type to be paged.
All Implemented Interfaces:
java.lang.Runnable

public abstract class PagedRequest<E>
extends java.lang.Thread

Base class for asynchronous paged requests.

Subclassing

Subclasses must implement the run() method, and do it fill the results list.

The implementation should correctly (and regularly) check for interruption condition, as it is the mechanism used by PagedRequestManager to cancel request executions.

Usage

A typical iteration loop over a paged request that gets the results, concurrently to the request execution, will have the following layout:

PageRequestManager<MyBean> myManager = new PageRequestManager<MyBean>(); ... PagedRequest<MyBean> req = myManager.getPagedRequest(id);

while (true) {
  try {
    MyBean items[] = req.getResults(results.size(), N);
    if (items == null) break; // No more items
    // do something whith items
  }
  catch (PagedRequestTimeoutException e) {
    // do nothing
  }
  catch (PagedRequestNoDataException e) {
    // sleep some time
  }
}

Or, if we just want to wait until the request execution is finished, and get all its results in one operation, we can do:

PagedRequest req = ...;
req.join();

int count = req.getResultsCount();
Object results[] = (count == 0) ? new Object[0] : req.getResults(0, count);

Version:
1.0
Author:
lgaspart, rruiz, Xmas (generics)

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  com.gridsystems.innergrid.kernel.server.KernelContext originalContext
          Request user information.
protected  java.util.ArrayList<E> results
          Results of the request.
protected  java.lang.Class<E> theClass
          Class of type E.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
PagedRequest(com.gridsystems.innergrid.kernel.server.KernelContext context, java.lang.Class<E> theClass, boolean blockRequest)
          Creates a new instance.
 
Method Summary
 void clear()
          Clears the Paged request.
 long getId()
          Gets the identifier of the paged request.
 long getLastAccessTime()
          Gets the time of the last access to this request data.
 E[] getResults(int from, int count)
          Calls getResultsTimeout() with an appropriate timeout value depending on the 'block' flag specified at construction time.
 int getResultsCount()
          Gets the number of results of the paged request.
 E[] getResultsTimeout(int from, int count, long timeout)
          Asks for 'count' results, starting at offset 'from'.
 long getTime()
          Gets the hour of the paged request.
 java.lang.String getUser()
          Gets the user of the paged request.
abstract  void run()
          Subclasses must implement the request in this method.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

originalContext

protected com.gridsystems.innergrid.kernel.server.KernelContext originalContext
Request user information.


results

protected java.util.ArrayList<E> results
Results of the request.


theClass

protected final java.lang.Class<E> theClass
Class of type E.

Constructor Detail

PagedRequest

public PagedRequest(com.gridsystems.innergrid.kernel.server.KernelContext context,
                    java.lang.Class<E> theClass,
                    boolean blockRequest)
Creates a new instance.

Parameters:
theClass - Class of type E
context - The request context
blockRequest - true if the request must be blocking
Method Detail

run

public abstract void run()
Subclasses must implement the request in this method.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

getId

public long getId()
Gets the identifier of the paged request.

Overrides:
getId in class java.lang.Thread
Returns:
this paged request identifier

getUser

public java.lang.String getUser()
Gets the user of the paged request.

Returns:
the user of this paged request

getTime

public long getTime()
Gets the hour of the paged request.

Returns:
the hour of this paged request

getResultsCount

public int getResultsCount()
Gets the number of results of the paged request.

Returns:
the number of results in this request

getLastAccessTime

public long getLastAccessTime()
Gets the time of the last access to this request data.

Returns:
the last access time

getResultsTimeout

public E[] getResultsTimeout(int from,
                             int count,
                             long timeout)
                      throws PagedRequestNoDataException
Asks for 'count' results, starting at offset 'from'. If not enough data is initially available, it will block during at most 'timeout' milliseconds.

The returned value depends on the following rules:

Parameters:
from - The offset of the first result to get
count - The number of results to get
timeout - The max blocking time
Returns:
An array with the obtained results, or null if the request is finished and 'from' is out of the results bounds.
Throws:
PagedRequestNoDataException - In case of timeout

getResults

public E[] getResults(int from,
                      int count)
               throws PagedRequestNoDataException
Calls getResultsTimeout() with an appropriate timeout value depending on the 'block' flag specified at construction time.

Parameters:
from - The initial offset
count - The number of results to retrieve
Returns:
An array with the results (either all, or a partial subset), or null if the request execution has finished and 'from' is out of bounds
Throws:
PagedRequestNoDataException - if no results are available yet

clear

public void clear()
Clears the Paged request.



Copyright © 2007-2008 Grid Systems, S.A.. All Rights Reserved.