KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbicommon > util > PeriodicTask


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.jbicommon.util;
23
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26
27 /**
28  * A polling util used to schedule recursive task to be executed. Extend this
29  * abstract class and implement the run method that will be launched each N ms.
30  *
31  * @author Christophe HAMERLING - eBMWebSourcing
32  *
33  */

34 public abstract class PeriodicTask extends TimerTask JavaDoc {
35
36     /**
37      * The timer used for task scheduling
38      */

39     protected Timer JavaDoc timer;
40
41     /**
42      * The polling period
43      */

44     protected long period;
45
46     /**
47      * The first task execution delay
48      */

49     protected long delay;
50
51     /**
52      * The default polling period
53      */

54     private static final long DEFAULT_PERIOD = 1000L;
55
56     /**
57      * Creates a new instance of {@link PeriodicTask}
58      *
59      */

60     public PeriodicTask() {
61         this(DEFAULT_PERIOD, DEFAULT_PERIOD);
62     }
63
64     /**
65      * Creates a new instance of {@link PeriodicTask}
66      *
67      * @param period
68      * in ms
69      */

70     public PeriodicTask(long delay, long period) {
71         this.delay = delay;
72         this.period = period;
73         timer = new Timer JavaDoc();
74     }
75
76     /**
77      * Start the task processing
78      *
79      */

80     public void startProcessing() {
81         timer.schedule(this, delay, period);
82     }
83
84     /**
85      * Stop the task processing
86      *
87      */

88     public void stopProcessing() {
89         timer.cancel();
90     }
91 }
92
Popular Tags