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.

To create a timer, you must invoke Timer's method notifyAfter(listener, timePeriod, event) and pass correct parameters to it.

> listener is object of a class implementing the TimerListener interface whose time(event) method will be invoked after the given timePeriod (in seconds) with event specified in the notifyAfter method. The Timer Service makes an event queue from all started timer listeners and when some timePeriod of them passes, it invokes its timer method with specified event and removes it from queue. If the notifyAfter method is invoked with listener and event that already exist in queue, the previous listener is removed and the new one replaces it.

> event is an int parameter. When more than one timer is started with the same listener, event shows which of them is to be notified.

With the removeLisener(listener, event) method it is possible to remove a listener with the specified event from the queue before its timePeriod has passed. If the listener with the specified event is not found, nothing happens.

> 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.

public class TestTimer { private BundleContext bc; private ServiceReference timerReference; private Timer timer; public TestTimer(BundleContext bc) { try { this.bc = bc; timerReference = bc.getServiceReference("org.eclipse.equinox.util.timer.Timer"); if (timerReference != null) { timer = (Timer)bc.getService(timerReference); setTimerIntervals(); } } catch (Exception e) { System.out.println("An error in start method.\n " + e); } } private void setTimerIntervals() { int t; //timePeriod int e; //event //create lsn1 lisener implementing TimerLisener interface TestTimerListener lsn1 = new TestTimerListener(); t = 10; e = 1; //start timer interval timer.notifyAfter(lsn1, t, e); System.out.println("Timer with lisener lsn1 was started - timePeriod " + t + " sec, event " + e); //create lsn2 lisener implementing TimerLisener interface TestTimerListener lsn2 = new TestTimerListener(); t = 12; e = 2; //start timer interval timer.notifyAfter(lsn2, t, e); System.out.println("Timer with lisener lsn2 was started - timePeriod " + t + " sec, event " + e); t = 5; e = 1; //start timer interval timer.notifyAfter(lsn1, t, e); System.out.println("Timer with lisener lsn1 was started - timePeriod " + t + " sec, event " + e); System.out.println("\n"); //remove lsn2 lisener with event 1 e = 2; timer.removeListener(lsn2, e); } }

> The TestTimerListener class implements the TimerListener interface and its method timer(event) will be invoked every time a timerPeriod passes.

public class TestTimerListener implements TimerListener { public TestTimerListener() { } public void timer(int event){ System.out.println("Timer with event " + event + " has ended"); } }