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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Vojtech Juranek
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.ExtensionList;
27 import hudson.ExtensionPoint;
28 import hudson.triggers.SafeTimerTask;
29 import hudson.triggers.Trigger;
30 import jenkins.model.Jenkins;
31
32 import java.util.Random;
33 import java.util.Timer;
34 import java.util.logging.Logger;
35
36
37
38 /**
39 * Extension point which allows scheduling a task with variable interval. Interval in evaluated every time before next
40 * task is scheduled by calling {@link #getRecurrencePeriod()}. Task to be scheduled is obtain by calling {@link #getNewInstance()}.
41 *
42 * <p>
43 * This class is similar to {@link PeriodicWork}. The main difference is in re-evaluating delay interval every time.
44 * See {@link PeriodicWork} for details. Analog of {@link AsyncPeriodicWork} is {@link AsyncAperiodicWork}.
45 *
46 * @author vjuranek
47 * @since 1.410
48 */
49 public abstract class AperiodicWork extends SafeTimerTask implements ExtensionPoint {
50
51 protected final Logger logger = Logger.getLogger(getClass().getName());
52
53 /**
54 * Gets the number of milliseconds between successive executions.
55 *
56 * <p>
57 * Jenkins calls this method every time the timer task is scheduled.
58 *
59 */
60 public abstract long getRecurrencePeriod();
61
62 /**
63 * Gets new instance of task to be executed. Method should return new instance each time, as there no check, if previously
64 * scheduled task already finished. Returning same instance could lead to throwing {@link IllegalStateException} (especially
65 * in case of {@link AsyncAperiodicWork}) and therefore scheduling of next tasks will be broken.
66 *
67 * @return AperiodicWork - timer task instance to be executed
68 */
69 public abstract AperiodicWork getNewInstance();
70
71 /**
72 * Gets the number of milliseconds till 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 @Override
86 public final void doRun() throws Exception{
87 doAperiodicRun();
88 Timer timer = Trigger.timer;
89 if (timer != null) {
90 timer.schedule(getNewInstance(), getRecurrencePeriod());
91 }
92 }
93
94 protected abstract void doAperiodicRun();
95
96 /**
97 * Returns all the registered {@link AperiodicWork}s.
98 */
99 public static ExtensionList<AperiodicWork> all() {
100 return Jenkins.getInstance().getExtensionList(AperiodicWork.class);
101 }
102
103 private static final Random RANDOM = new Random();
104 }
105