1 7 8 package com.sun.jmx.remote.internal; 9 10 import java.io.IOException ; 11 12 import com.sun.jmx.remote.util.ClassLogger; 13 14 public abstract class ServerCommunicatorAdmin { 15 public ServerCommunicatorAdmin(long timeout) { 16 if (logger.traceOn()) { 17 logger.trace("Constructor", 18 "Creates a new ServerCommunicatorAdmin object "+ 19 "with the timeout "+timeout); 20 } 21 22 this.timeout = timeout; 23 24 timestamp = 0; 25 if (timeout < Long.MAX_VALUE) { 26 Runnable timeoutTask = new Timeout(); 27 final Thread t = new Thread (timeoutTask); 28 t.setName("JMX server connection timeout " + t.getId()); 29 t.setDaemon(true); 32 t.start(); 33 } 34 } 35 36 45 public boolean reqIncoming() { 46 if (logger.traceOn()) { 47 logger.trace("reqIncoming", "Receive a new request."); 48 } 49 50 synchronized(lock) { 51 if (terminated) { 52 logger.warning("reqIncoming", 53 "The server has decided to close " + 54 "this client connection."); 55 } 56 ++currentJobs; 57 58 return terminated; 59 } 60 } 61 62 68 public boolean rspOutgoing() { 69 if (logger.traceOn()) { 70 logger.trace("reqIncoming", "Finish a request."); 71 } 72 73 synchronized(lock) { 74 if (--currentJobs == 0) { 75 timestamp = System.currentTimeMillis(); 76 logtime("Admin: Timestamp=",timestamp); 77 lock.notify(); 79 } 80 return terminated; 81 } 82 } 83 84 87 protected abstract void doStop(); 88 89 93 public void terminate() { 94 if (logger.traceOn()) { 95 logger.trace("terminate", 96 "terminate the ServerCommunicatorAdmin object."); 97 } 98 99 synchronized(lock) { 100 if (terminated) { 101 return; 102 } 103 104 terminated = true; 105 106 lock.notify(); 108 } 109 } 110 111 private class Timeout implements Runnable { 115 public void run() { 116 boolean stopping = false; 117 118 synchronized(lock) { 119 if (timestamp == 0) timestamp = System.currentTimeMillis(); 120 logtime("Admin: timeout=",timeout); 121 logtime("Admin: Timestamp=",timestamp); 122 123 while(!terminated) { 124 try { 125 while(!terminated && currentJobs != 0) { 127 if (logger.traceOn()) { 128 logger.trace("Timeout-run", 129 "Waiting without timeout."); 130 } 131 132 lock.wait(); 133 } 134 135 if (terminated) return; 136 137 final long remaining = 138 timeout - (System.currentTimeMillis() - timestamp); 139 140 logtime("Admin: remaining timeout=",remaining); 141 142 if (remaining > 0) { 143 144 if (logger.traceOn()) { 145 logger.trace("Timeout-run", 146 "Waiting with timeout: "+ 147 remaining + " ms remaining"); 148 } 149 150 lock.wait(remaining); 151 } 152 153 if (currentJobs > 0) continue; 154 155 final long elapsed = 156 System.currentTimeMillis() - timestamp; 157 logtime("Admin: elapsed=",elapsed); 158 159 if (!terminated && elapsed > timeout) { 160 if (logger.traceOn()) { 161 logger.trace("Timeout-run", 162 "timeout elapsed"); 163 } 164 logtime("Admin: timeout elapsed! "+ 165 elapsed+">",timeout); 166 terminated = true; 168 169 stopping = true; 170 break; 171 } 172 } catch (InterruptedException ire) { 173 logger.warning("Timeout-run","Unexpected Exception: "+ 174 ire); 175 logger.debug("Timeout-run",ire); 176 return; 177 } 178 } 179 } 180 181 if (stopping) { 182 if (logger.traceOn()) { 183 logger.trace("Timeout-run", "Call the doStop."); 184 } 185 186 doStop(); 187 } 188 } 189 } 190 191 private void logtime(String desc,long time) { 192 timelogger.trace("synchro",desc+time); 193 } 194 195 private long timestamp; 199 200 private final int[] lock = new int[0]; 201 private int currentJobs = 0; 202 203 private long timeout; 204 205 private boolean terminated = false; 207 208 private static final ClassLogger logger = 209 new ClassLogger("javax.management.remote.misc", 210 "ServerCommunicatorAdmin"); 211 private static final ClassLogger timelogger = 212 new ClassLogger("javax.management.remote.timeout", 213 "ServerCommunicatorAdmin"); 214 } 215 | Popular Tags |