The Timer Class


You want to continually monitor a web site over time to see if it contains the search terms you specify. You're interested in checking every several minutes or perhaps every several seconds.


The test testRepeatedSearch in SearchSchedulerTest shows the intent of how this search monitor should work: Create a scheduler and pass to it a search plus an interval representing how often to run the search. The test mechanics involve waiting for a set period of time once the scheduler has started and then verifying that the expected number of searches are executed within that time.

 package sis.search; import junit.framework.*; import sis.util.*; public class SearchSchedulerTest extends TestCase {    private int actualResultsCount = 0;    protected void setUp() throws Exception {       TestUtil.delete(SearchTest.FILE);       LineWriter.write(SearchTest.FILE, SearchTest.TEST_HTML);    }    protected void tearDown() throws Exception {       TestUtil.delete(SearchTest.FILE);    }    public void testRepeatedSearch() throws Exception {       final int searchInterval = 3000;       Search search = new Search(SearchTest.URL, "xxx");       ResultsListener listener = new ResultsListener() {          public void executed(Search search) {             ++actualResultsCount;          }};       SearchScheduler scheduler = new SearchScheduler(listener);       scheduler.repeat(search, searchInterval);       final int expectedResultsCount = 3;       Thread.sleep((expectedResultsCount - 1) * searchInterval + 1000);       scheduler.stop();       assertEquals(expectedResultsCount, actualResultsCount);    } } 

The SearchScheduler class, below, uses the java.util.Timer class as the basis for scheduling and executing searches. Once you have created a Timer instance, you can call one of a handful of methods to schedule TimerTasks. A TimerTask is an abstract class that implements the Runnable interface. You supply the details on what the task does by subclassing TimerTask and implementing the run method.

In SearchScheduler, you use the method scheduleAtFixedRate, which you pass a TimerTask, a delay before start of 0 milliseconds, and an interval (currently specified by the test) in milliseconds. Every interval milliseconds, the Timer object wakes up and sends the run message to the task. This is known as fixed-rate execution.

When you use fixed-rate execution,[16] the Timer does its best to ensure that tasks are run every interval milliseconds. If a task takes longer than the interval, the Timer tries to execute tasks more tightly together to ensure that they finish before each interval is up. You can also use one of the delayed-rate execution methods, in which the case the interval from the end of one search to the next remains consistent.

[16] [Sun2004b].

You can cancel both timers and timer tasks via the cancel method.

 package sis.search; import java.util.*; public class SearchScheduler {    private ResultsListener listener;    private Timer timer;    public SearchScheduler(ResultsListener listener) {       this.listener = listener;    }    public void repeat(final Search search, long interval) {       timer = new Timer();       TimerTask task = new TimerTask()x` {          public void run() {             search.execute();             listener.executed(search);          }       };       timer.scheduleAtFixedRate(task, 0, interval);    }    public void stop() {       timer.cancel();    } } 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net