6.3 Exception Handling in Threadable Tasks

One advantage of using threadable tasks is that unlike `raw' threads there is a well defined way of handling exceptions which occur during execution. These are automatically caught by the Reaction framework and converted to deferred errback calls. The error condition may then be handled in the same manner as conventional deferred errbacks. An example of a long running task which generates an exception is shown in Listing 6.5.


\begin{listing}
% latex2html id marker 1709\begin{small}\begin{verbatim}publ...
...l}\caption{Throwing Exception From a Threadable Long Running Task}
\end{listing}

In this example an exception is explicitly thrown by the threadable run method, and the generated exception object will be passed back via the deferred errback chain. The full implementation of this example is provided in the thread examples package as ThreadableExample2. This includes a deferrable callback handler which prints out the details of the generated exception, as shown in Listing 6.6.


\begin{listing}
% latex2html id marker 1721\begin{small}\begin{verbatim}INFO...
...nd{small}\caption{Console Output of Threadable Exception Handling}
\end{listing}

It is worth noting from the debug log messages that additional worker threads are automatically created and destroyed in order to execute the threadable task code. These are standard Java threads which are managed within the reactor core as a dynamic thread pool. By reusing threads from the thread pool, the overhead of creating and destroying thread objects for each execution of a threadable task is much reduced.