org.elkoserver.foundation.run
Class SlowServiceRunner
java.lang.Object
org.elkoserver.foundation.run.SlowServiceRunner
public class SlowServiceRunner
- extends Object
This class provides a mechanism for safely making use of external services
that are only available via synchronous, blocking (i.e., slow) interfaces.
It maintains a thread pool in which calls to such services run, delivering
their results back via callback thunks that are dropped onto the normal
server run queue.
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SlowServiceRunner
public SlowServiceRunner(Runner resultRunner,
int maxPoolSize)
- Constructor. Note that while this is a constructable class, in practice
it should usually be managed as a singleton, i.e., don't mak more than
one.
- Parameters:
resultRunner - Run queue in which result handlers will be runmaxPoolSize - Maximum number of threads allowed in the thread pool
enqueueTask
public void enqueueTask(Callable<Object> task,
ArgRunnable resultHandler)
- Enqueue a task to be executed via the slow path. Unlike most code
running in our environment, tasks run via this mechanism *are* allowed
to invoke operations that can block. However, they are not allowed to
lock or reference any normal mutable server state. Since there is not
really any graceful way to enforce the latter rule, tasks are on their
honor to be well behaved!
Tasks should return a result object that will be passed to the given
result handler for execution in the main server run queue. It is
permissible for a task result to be null. However, if a task throws an
exception, this will be given to the handler as the result, so if the
normal task result is an exception type, it is up to the programmer of
the task and the result handler to take measures to sort things out
appropriately.
- Parameters:
task - Callable that executes the task. This will be executed in
a separate thread.resultHandler - Thunk that will be invoked with the result
returned by the task. This will be executed on the main run queue.