![]() | ![]() | ![]() |
| |||||||||||
![]() | ||||||||||||||
![]() | ![]() | |||||||||||||
Resources can use the JCA timer capability to manage periodic tasks.
The timers use the familiar Timers start short tasks. Longer timed tasks will use the timer in combination with the JCA work management API. Files in this tutorial
The Timer taskThe Resin provides the The public class WorkScheduleTimerTask extends java.util.TimerTask { private static final Logger log = Logger.getLogger(TimerTask.class.getName()); private WorkManager _workManager; private Work _work; WorkScheduleTimerTask(WorkManager workManager, Work work) { _workManager = workManager; _work = work; } public void run() { try { _workManager.scheduleWork(_work); } catch (WorkException e) { log.log(Level.WARNING, e.toString(), e); } } } The work taskFor this example, the work task is trivial. It just increments a
counter in the public class WorkTask implements Work { private TimerResource _resource; WorkTask(TimerResource resource) { _resource = resource; } public void run() { _resource.addCount(); } public void release() { } } The ResourceThe public class TimerResource extends AbstractResourceAdapter { private int _count; /** * Adds to the count. */ public void addCount() { _count++; } public void start(BootstrapContext ctx) throws ResourceAdapterInternalException { WorkManager workManager = ctx.getWorkManager(); Work work = new WorkTask(this); TimerTask timerTask = new WorkScheduleTimerTask(workManager, work); Timer timer = ctx.createTimer(); long initialDelay = 0; long period = 10000L; timer.schedule(timerTask, initialDelay, period); } public void stop() throws ResourceAdapterInternalException { } public String toString() { return "TimerResource[" + _count + "]"; } } Configuration and JSPThe configuration for this resource is trivial since it has no attributes. <resource name="jca/timer" type="example.TimerResource"/> The demo JSP is also trivial. It looks up the resource through JNDI and prints it to the page. <%@ page import="javax.naming.*" %> <%= new InitialContext().lookup("java:comp/env/jca/timer") %>
|