1004
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.jmx.event;
|
|
27 |
|
|
28 |
import com.sun.jmx.remote.util.ClassLogger;
|
|
29 |
import java.util.concurrent.Executors;
|
|
30 |
import java.util.concurrent.FutureTask;
|
|
31 |
import java.util.concurrent.ScheduledExecutorService;
|
|
32 |
import java.util.concurrent.ScheduledFuture;
|
|
33 |
import java.util.concurrent.TimeUnit;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* <p>Manage a renewable lease. The lease can be renewed indefinitely
|
|
37 |
* but if the lease runs to its current expiry date without being renewed
|
|
38 |
* then the expiry callback is invoked. If the lease has already expired
|
|
39 |
* when renewal is attempted then the lease method returns zero.</p>
|
|
40 |
* @author sjiang
|
|
41 |
* @author emcmanus
|
|
42 |
*/
|
|
43 |
// The synchronization logic of this class is tricky to deal correctly with the
|
|
44 |
// case where the lease expires at the same time as the |lease| or |stop| method
|
|
45 |
// is called. If the lease is active then the field |scheduled| represents
|
|
46 |
// the expiry task; otherwise |scheduled| is null. Renewing or stopping the
|
|
47 |
// lease involves canceling this task and setting |scheduled| either to a new
|
|
48 |
// task (to renew) or to null (to stop).
|
|
49 |
//
|
|
50 |
// Suppose the expiry task runs at the same time as the |lease| method is called.
|
|
51 |
// If the task enters its synchronized block before the method starts, then
|
|
52 |
// it will set |scheduled| to null and the method will return 0. If the method
|
|
53 |
// starts before the task enters its synchronized block, then the method will
|
|
54 |
// cancel the task which will see that when it later enters the block.
|
|
55 |
// Similar reasoning applies to the |stop| method. It is not expected that
|
|
56 |
// different threads will call |lease| or |stop| simultaneously, although the
|
|
57 |
// logic should be correct then too.
|
|
58 |
public class LeaseManager {
|
|
59 |
public LeaseManager(Runnable callback) {
|
|
60 |
this(callback, EventParams.getLeaseTimeout());
|
|
61 |
}
|
|
62 |
|
|
63 |
public LeaseManager(Runnable callback, long timeout) {
|
|
64 |
if (logger.traceOn()) {
|
|
65 |
logger.trace("LeaseManager", "new manager with lease: "+timeout);
|
|
66 |
}
|
|
67 |
if (callback == null) {
|
|
68 |
throw new NullPointerException("Null callback.");
|
|
69 |
}
|
|
70 |
if (timeout <= 0)
|
|
71 |
throw new IllegalArgumentException("Timeout must be positive: " + timeout);
|
|
72 |
|
|
73 |
this.callback = callback;
|
|
74 |
schedule(timeout);
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* <p>Renew the lease for the given time. The new time can be shorter
|
|
79 |
* than the previous one, in which case the lease will expire earlier
|
|
80 |
* than it would have.</p>
|
|
81 |
*
|
|
82 |
* <p>Calling this method after the lease has expired will return zero
|
|
83 |
* immediately and have no other effect.</p>
|
|
84 |
*
|
|
85 |
* @param timeout the new lifetime. If zero, the lease
|
|
86 |
* will expire immediately.
|
|
87 |
*/
|
|
88 |
public synchronized long lease(long timeout) {
|
|
89 |
if (logger.traceOn()) {
|
|
90 |
logger.trace("lease", "new lease to: "+timeout);
|
|
91 |
}
|
|
92 |
|
|
93 |
if (timeout < 0)
|
|
94 |
throw new IllegalArgumentException("Negative lease: " + timeout);
|
|
95 |
|
|
96 |
if (scheduled == null)
|
|
97 |
return 0L;
|
|
98 |
|
|
99 |
scheduled.cancel(false);
|
|
100 |
|
|
101 |
if (logger.traceOn())
|
|
102 |
logger.trace("lease", "start lease: "+timeout);
|
|
103 |
schedule(timeout);
|
|
104 |
|
|
105 |
return timeout;
|
|
106 |
}
|
|
107 |
|
|
108 |
private class Expire implements Runnable {
|
|
109 |
ScheduledFuture<?> task;
|
|
110 |
|
|
111 |
public void run() {
|
|
112 |
synchronized (LeaseManager.this) {
|
|
113 |
if (task.isCancelled())
|
|
114 |
return;
|
|
115 |
scheduled = null;
|
|
116 |
}
|
|
117 |
callback.run();
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
private synchronized void schedule(long timeout) {
|
|
122 |
Expire expire = new Expire();
|
|
123 |
scheduled = executor.schedule(expire, timeout, TimeUnit.MILLISECONDS);
|
|
124 |
expire.task = scheduled;
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* <p>Cancel the lease without calling the expiry callback.</p>
|
|
129 |
*/
|
|
130 |
public synchronized void stop() {
|
|
131 |
logger.trace("stop", "canceling lease");
|
|
132 |
scheduled.cancel(false);
|
|
133 |
scheduled = null;
|
|
134 |
}
|
|
135 |
|
|
136 |
private final Runnable callback;
|
|
137 |
private ScheduledFuture scheduled; // If null, the lease has expired.
|
|
138 |
|
|
139 |
private final ScheduledExecutorService executor
|
|
140 |
= Executors.newScheduledThreadPool(1,
|
|
141 |
new DaemonThreadFactory("LeaseManager"));
|
|
142 |
|
|
143 |
private static final ClassLogger logger =
|
|
144 |
new ClassLogger("javax.management.event", "LeaseManager");
|
|
145 |
|
|
146 |
}
|