History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/jmx/remote/internal/ClientCommunicatorAdmin.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/jmx/remote/internal/ClientCommunicatorAdmin.java+252−0
@@ -0,0 +1,252 @@
1+/*
2+ * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.jmx.remote.internal;
27+
28+import java.io.IOException;
29+import java.io.InterruptedIOException;
30+
31+import com.sun.jmx.remote.util.ClassLogger;
32+import com.sun.jmx.remote.util.EnvHelp;
33+
34+public abstract class ClientCommunicatorAdmin {
35+ private static volatile long threadNo = 1;
36+
37+ public ClientCommunicatorAdmin(long period) {
38+ this.period = period;
39+
40+ if (period > 0) {
41+ checker = new Checker();
42+
43+ Thread t = new Thread(checker, "JMX client heartbeat " + ++threadNo);
44+ t.setDaemon(true);
45+ t.start();
46+ } else
47+ checker = null;
48+ }
49+
50+ /**
51+ * Called by a client to inform of getting an IOException.
52+ */
53+ public void gotIOException (IOException ioe) throws IOException {
54+ restart(ioe);
55+ }
56+
57+ /**
58+ * Called by this class to check a client connection.
59+ */
60+ protected abstract void checkConnection() throws IOException;
61+
62+ /**
63+ * Tells a client to re-start again.
64+ */
65+ protected abstract void doStart() throws IOException;
66+
67+ /**
68+ * Tells a client to stop because failing to call checkConnection.
69+ */
70+ protected abstract void doStop();
71+
72+ /**
73+ * Terminates this object.
74+ */
75+ public void terminate() {
76+ synchronized(lock) {
77+ if (state == TERMINATED) {
78+ return;
79+ }
80+
81+ state = TERMINATED;
82+
83+ lock.notifyAll();
84+
85+ if (checker != null)
86+ checker.stop();
87+ }
88+ }
89+
90+ private void restart(IOException ioe) throws IOException {
91+ // check state
92+ synchronized(lock) {
93+ if (state == TERMINATED) {
94+ throw new IOException("The client has been closed.");
95+ } else if (state == FAILED) { // already failed to re-start by another thread
96+ throw ioe;
97+ } else if (state == RE_CONNECTING) {
98+ // restart process has been called by another thread
99+ // we need to wait
100+ while(state == RE_CONNECTING) {
101+ try {
102+ lock.wait();
103+ } catch (InterruptedException ire) {
104+ // be asked to give up
105+ InterruptedIOException iioe = new InterruptedIOException(ire.toString());
106+ EnvHelp.initCause(iioe, ire);
107+
108+ throw iioe;
109+ }
110+ }
111+
112+ if (state == TERMINATED) {
113+ throw new IOException("The client has been closed.");
114+ } else if (state != CONNECTED) {
115+ // restarted is failed by another thread
116+ throw ioe;
117+ }
118+ } else {
119+ state = RE_CONNECTING;
120+ lock.notifyAll();
121+ }
122+ }
123+
124+ // re-starting
125+ try {
126+ doStart();
127+ synchronized(lock) {
128+ if (state == TERMINATED) {
129+ throw new IOException("The client has been closed.");
130+ }
131+
132+ state = CONNECTED;
133+
134+ lock.notifyAll();
135+ }
136+
137+ return;
138+ } catch (Exception e) {
139+ logger.warning("restart", "Failed to restart: " + e);
140+ logger.debug("restart",e);
141+
142+ synchronized(lock) {
143+ if (state == TERMINATED) {
144+ throw new IOException("The client has been closed.");
145+ }
146+
147+ state = FAILED;
148+
149+ lock.notifyAll();
150+ }
151+
152+ try {
153+ doStop();
154+ } catch (Exception eee) {
155+ // OK.
156+ // We know there is a problem.
157+ }
158+
159+ terminate();
160+
161+ throw ioe;
162+ }
163+ }
164+
165+// --------------------------------------------------------------
166+// private varaibles
167+// --------------------------------------------------------------
168+ private class Checker implements Runnable {
169+ public void run() {
170+ myThread = Thread.currentThread();
171+
172+ while (state != TERMINATED && !myThread.isInterrupted()) {
173+ try {
174+ Thread.sleep(period);
175+ } catch (InterruptedException ire) {
176+ // OK.
177+ // We will check the state at the following steps
178+ }
179+
180+ if (state == TERMINATED || myThread.isInterrupted()) {
181+ break;
182+ }
183+
184+ try {
185+ checkConnection();
186+ } catch (Exception e) {
187+ synchronized(lock) {
188+ if (state == TERMINATED || myThread.isInterrupted()) {
189+ break;
190+ }
191+ }
192+
193+ e = (Exception)EnvHelp.getCause(e);
194+
195+ if (e instanceof IOException &&
196+ !(e instanceof InterruptedIOException)) {
197+ try {
198+ restart((IOException)e);
199+ } catch (Exception ee) {
200+ logger.warning("Checker-run",
201+ "Failed to check connection: "+ e);
202+ logger.warning("Checker-run", "stopping");
203+ logger.debug("Checker-run",e);
204+
205+ break;
206+ }
207+ } else {
208+ logger.warning("Checker-run",
209+ "Failed to check the connection: " + e);
210+ logger.debug("Checker-run",e);
211+
212+ // XXX stop checking?
213+
214+ break;
215+ }
216+ }
217+ }
218+
219+ if (logger.traceOn()) {
220+ logger.trace("Checker-run", "Finished.");
221+ }
222+ }
223+
224+ private void stop() {
225+ if (myThread != null && myThread != Thread.currentThread()) {
226+ myThread.interrupt();
227+ }
228+ }
229+
230+ private Thread myThread;
231+ }
232+
233+// --------------------------------------------------------------
234+// private variables
235+// --------------------------------------------------------------
236+ private final Checker checker;
237+ private long period;
238+
239+ // state
240+ private final static int CONNECTED = 0;
241+ private final static int RE_CONNECTING = 1;
242+ private final static int FAILED = 2;
243+ private final static int TERMINATED = 3;
244+
245+ private int state = CONNECTED;
246+
247+ private final int[] lock = new int[0];
248+
249+ private static final ClassLogger logger =
250+ new ClassLogger("javax.management.remote.misc",
251+ "ClientCommunicatorAdmin");
252+}