File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\PeriodicWork.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.triggers.SafeTimerTask;
27 import hudson.triggers.Trigger;
28 import hudson.ExtensionPoint;
29 import hudson.Extension;
30 import hudson.ExtensionList;
31 import jenkins.model.Jenkins;
32
33 import java.util.logging.Logger;
34 import java.util.Random;
35 import java.util.Timer;
36
37 /**
38 * Extension point to perform a periodic task in Hudson (through {@link Timer}.)
39 *
40 * <p>
41 * This extension point is useful if your plugin needs to perform some work in the background periodically
42 * (for example, monitoring, batch processing, garbage collection, etc.)
43 *
44 * <p>
45 * Put {@link Extension} on your class to have it picked up and registered automatically, or
46 * manually insert this to {@link Trigger#timer}.
47 *
48 * <p>
49 * This class is designed to run a short task. Implementations whose periodic work takes a long time
50 * to run should extend from {@link AsyncPeriodicWork} instead.
51 *
52 * @author Kohsuke Kawaguchi
53 * @see AsyncPeriodicWork
54 */
55 public abstract class PeriodicWork extends SafeTimerTask implements ExtensionPoint {
56 protected final Logger logger = Logger.getLogger(getClass().getName());
57
58 /**
59 * Gets the number of milliseconds between successive executions.
60 *
61 * <p>
62 * Hudson calls this method once to set up a recurring timer, instead of
63 * calling this each time after the previous execution completed. So this class cannot be
64 * used to implement a non-regular recurring timer.
65 *
66 * <p>
67 * IOW, the method should always return the same value.
68 */
69 public abstract long getRecurrencePeriod();
70
71 /**
72 * Gets the number of milliseconds til the first execution.
73 *
74 * <p>
75 * By default it chooses the value randomly between 0 and {@link #getRecurrencePeriod()}
76 */
77 public long getInitialDelay() {
78 long l = RANDOM.nextLong();
79 // Math.abs(Long.MIN_VALUE)==Long.MIN_VALUE!
80 if (l==Long.MIN_VALUE)
81 l++;
82 return Math.abs(l)%getRecurrencePeriod();
83 }
84
85 /**
86 * Returns all the registered {@link PeriodicWork}s.
87 */
88 public static ExtensionList<PeriodicWork> all() {
89 return Jenkins.getInstance().getExtensionList(PeriodicWork.class);
90 }
91
92 // time constants
93 protected static final long MIN = 1000*60;
94 protected static final long HOUR =60*MIN;
95 protected static final long DAY = 24*HOUR;
96
97 private static final Random RANDOM = new Random();
98 }
99