| <html> |
| |
| <head> |
| <meta http-equiv="Content-Type" |
| content="text/html; charset=iso-8859-1"> |
| <meta name="Author" content="Maria Ivanova"> |
| <meta name="GENERATOR" content="Microsoft FrontPage Express 2.0"> |
| <title>org.eclipse.equinox.util.timer</title> |
| </head> |
| |
| <body> |
| |
| <p>This package adds the Timer Service by |
| adding two additional interfaces. The idea is the Timer Service |
| to provide universal timers which can be used by other bundles if |
| they need them. When the Util Bundle is started, the Timer |
| Service is registered and started, too. To use the Timer Service, |
| you have to get it from the Bundle Context. </p> |
| |
| <p>To create a timer, you must invoke Timer's method <i>notifyAfter(listener, |
| timePeriod, event)</i> and pass correct parameters to it. </p> |
| |
| <blockquote> |
| <p><i>listener</i> is object of a class implementing the<i> |
| TimerListener</i> interface whose <i>time(event)</i> method |
| will be invoked after the given <i>timePeriod</i> (in seconds) |
| with event specified in the <i>notifyAfter</i> method. The |
| Timer Service makes an event queue from all started timer |
| listeners and when some <i>timePeriod</i> of them passes, it |
| invokes its timer method with specified <i>event</i> and |
| removes it from queue. If the <i>notifyAfter</i> method is |
| invoked with listener and event that already exist in queue, |
| the previous listener is removed and the new one replaces it.</p> |
| </blockquote> |
| |
| <blockquote> |
| <p><i>event</i> is an int parameter. When more than one timer |
| is started with the same listener, <i>event</i> shows which |
| of them is to be notified.</p> |
| </blockquote> |
| |
| <p>With the <i>removeLisener(listener, event)</i> method it is |
| possible to remove a listener with the specified event from the |
| queue before its <i>timePeriod</i> has passed. If the listener |
| with the specified event is not found, nothing happens. <br> |
| </p> |
| |
| <blockquote> |
| <p>Here is an example which starts 3 timers with diferent |
| timerPeriod. The timer with lisener lsn1 and event 1 will be |
| started again with diferent timePeriod, the Timer Service |
| will remove the previous timer and only new one will stay in |
| the queue. The timer with lisener lsn2 and event 2 will be |
| removed from the queue befor its timePeriod has passed. Only |
| timer with lisener lsn1, timePeriod 5 sec and event 1 will be |
| notified by the Timer Service when its timePeriod has passed.</p> |
| </blockquote> |
| |
| <table border="1" width="100%" cols="1"> |
| <tr> |
| <td width="100%" nowrap><blockquote> |
| <p> <br> |
| <font size="2" face="Courier New">public class |
| TestTimer { <br> |
| private BundleContext bc; <br> |
| private ServiceReference timerReference; <br> |
| private Timer timer; </font></p> |
| <p><font size="2" face="Courier New"> public |
| TestTimer(BundleContext bc) { <br> |
| try { <br> |
| this.bc = bc; <br> |
| timerReference = bc.getServiceReference("org.eclipse.equinox.util.timer.Timer"); |
| <br> |
| if (timerReference != |
| null) { <br> |
| timer = (Timer)bc.getService(timerReference); |
| <br> |
| |
| setTimerIntervals(); |
| <br> |
| } <br> |
| } catch (Exception e) { <br> |
| System.out.println("An |
| error in start method.\n " + e); <br> |
| } <br> |
| } <br> |
| <br> |
| private void setTimerIntervals() { <br> |
| int t; //timePeriod <br> |
| int e; //event <br> |
| <br> |
| //create lsn1 lisener implementing |
| TimerLisener interface <br> |
| TestTimerListener lsn1 = new |
| TestTimerListener(); <br> |
| t = 10; <br> |
| e = 1; <br> |
| //start timer interval <br> |
| timer.notifyAfter(lsn1, t, e); <br> |
| System.out.println("Timer |
| with lisener lsn1 was started - timePeriod " + t |
| + " sec, event " + e); <br> |
| <br> |
| //create lsn2 lisener implementing |
| TimerLisener interface <br> |
| TestTimerListener lsn2 = new |
| TestTimerListener(); <br> |
| t = 12; <br> |
| e = 2; <br> |
| //start timer interval <br> |
| timer.notifyAfter(lsn2, t, e); <br> |
| System.out.println("Timer |
| with lisener lsn2 was started - timePeriod " + t |
| + " sec, event " + e); </font></p> |
| <p><font size="2" face="Courier New"> |
| t = 5; <br> |
| e = 1; <br> |
| //start timer interval <br> |
| timer.notifyAfter(lsn1, t, e); <br> |
| System.out.println("Timer |
| with lisener lsn1 was started - timePeriod " + t |
| + " sec, event " + e); <br> |
| System.out.println("\n"); |
| <br> |
| <br> |
| //remove lsn2 lisener with event 1 |
| <br> |
| e = 2; <br> |
| timer.removeListener(lsn2, e); |
| <br> |
| } <br> |
| <br> |
| } <br> |
| </font></p> |
| </blockquote> |
| </td> |
| </tr> |
| </table> |
| |
| <blockquote> |
| <p>The TestTimerListener class implements the TimerListener |
| interface and its method timer(event) will be invoked every |
| time a timerPeriod passes.</p> |
| </blockquote> |
| |
| <table border="1" width="100%" cols="1"> |
| <tr> |
| <td><blockquote> |
| <p><font size="2" face="Courier New"> <br> |
| public class TestTimerListener implements |
| TimerListener { <br> |
| <br> |
| public TestTimerListener() { <br> |
| } <br> |
| <br> |
| public void timer(int event){ <br> |
| System.out.println("Timer |
| with event " + event + " has ended"); <br> |
| } <br> |
| }</font></p> |
| </blockquote> |
| </td> |
| </tr> |
| </table> |
| |
| <p><br> |
| <br> |
| <br> |
| <br> |
| <br> |
| <br> |
| </p> |
| </body> |
| </html> |