![]() | ![]() | ![]() |
| |||||||||
![]() | ||||||||||||
![]() | ![]() | |||||||||||
Resources using threads will want to use JCA's work management API. The work management API lets Resin manage threads for the resource rather than forcing the resource to manage its own threads. Since Resin is in a better position to manager threads, using the Work API is not only a convenience, but is a cleaner and more reliable implementation. Files in this tutorial
The Work taskA The example task increments a counter and sleeps.
The The only complexity in the task implements the The task uses import javax.resource.Work; public class WorkTask implements Work { private volatile boolean _isActive = true; private int _count; int getCount() { return _count; } public void run() { while (_isActive) { _count++; try { synchronized (this) { wait(10000); } } catch (Throwable e) { } } } public void release() { _isActive = false; try { synchronized (this) { notifyAll(); } } catch (Throwable e) { } } The ResourceThe import com.caucho.jca.AbstractResourceAdapter; public class WorkResource extends AbstractResourceAdapter { public void start(BootstrapContext ctx) throws ResourceAdapterInternalException { WorkTask work = new WorkTask(); WorkManager workManager = ctx.getWorkManager(); try { // Submits the work, but does not wait for the result. // In other words, it spawns a new thread workManager.startWork(work); } catch (WorkException e) { throw new ResourceAdapterInternalException(e); } } public String toString() { return "WorkResource[" + _workTask.getCount() + "]"; } }
|