1 22 package org.xsocket.stream.io.impl; 23 24 import java.lang.management.ManagementFactory ; 25 import java.util.List ; 26 import java.util.concurrent.ThreadPoolExecutor ; 27 import java.util.concurrent.TimeUnit ; 28 import java.util.logging.Logger ; 29 30 import javax.management.JMException ; 31 import javax.management.ObjectName ; 32 33 import org.xsocket.Dispatcher; 34 import org.xsocket.IDispatcher; 35 import org.xsocket.IWorkerPool; 36 import org.xsocket.IntrospectionBasedDynamicBean; 37 import org.xsocket.stream.MultithreadedServer; 38 import org.xsocket.stream.io.spi.IAcceptor; 39 import org.xsocket.stream.io.spi.IServerIoJmxProvider; 40 41 42 43 51 public final class JmxIoProvider implements IServerIoJmxProvider { 52 53 public Object createMBean(MultithreadedServer server, IAcceptor acceptor, String domain) throws JMException { 54 55 if (acceptor instanceof Acceptor) { 56 57 Acceptor acptr = (Acceptor) acceptor; 58 Listener listener = new Listener(server, acptr, domain); 59 acptr.getDispatcherPool().addListener(listener); 60 61 for (IDispatcher dispatcher : acptr.getDispatchers()) { 62 try { 63 listener.onDispatcherAdded((Dispatcher) dispatcher); 64 } catch(Exception ignore) { } 65 } 66 67 68 return new IntrospectionBasedDynamicBean(new ServerManagementBean(server, acptr)); 69 70 } else { 71 return new IntrospectionBasedDynamicBean(server); 72 } 73 } 74 75 76 private static final class Listener implements IIoSocketDispatcherPoolListener { 77 78 79 private MultithreadedServer server = null; 80 private String domain = null; 81 82 Listener(MultithreadedServer server, Acceptor acceptor, String domain) { 83 this.server = server; 84 this.domain = domain; 85 } 86 87 88 public void onWorkerPoolUpdated(IWorkerPool oldWorkerPool, IWorkerPool newWorkerPool) { 89 90 } 91 92 93 public void onDispatcherAdded(IDispatcher dispatcher) { 94 try { 95 ObjectName objectName = new ObjectName (domain +":type=IoSocketDispatcher,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort() + "." + dispatcher.hashCode()); 96 ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(dispatcher), objectName); 97 } catch (Exception ignore) { } 98 } 99 100 101 public void onDispatcherRemoved(IDispatcher dispatcher) { 102 try { 103 ObjectName objectName = new ObjectName (domain +":type=IoSocketDispatcher,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort() + "." + dispatcher.hashCode()); 104 ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName); 105 } catch (Exception ignore) { } 106 } 107 } 108 109 110 111 112 private static final class ServerManagementBean { 113 114 private MultithreadedServer server = null; 115 private Acceptor acceptor = null; 116 117 122 ServerManagementBean(MultithreadedServer server, Acceptor acceptor) { 123 this.server = server; 124 this.acceptor = acceptor; 125 } 126 127 128 134 public void setReceiveBufferPreallocationSize(int size) { 135 acceptor.setReceiveBufferPreallocationSize(size); 136 } 137 138 139 145 public int getReceiveBufferPreallocationSize() { 146 return acceptor.getReceiveBufferPreallocationSize(); 147 } 148 149 150 151 152 157 public int getIdleTimeoutSec() { 158 return server.getIdleTimeoutSec(); 159 } 160 161 162 163 168 public void setIdleTimeoutSec(int timeoutInSec) { 169 server.setIdleTimeoutSec(timeoutInSec); 170 } 171 172 173 178 public int getConnectionTimeoutSec() { 179 return server.getConnectionTimeoutSec(); 180 } 181 182 183 190 public void setConnectionTimeoutSec(int timeoutSec) { 191 server.setConnectionTimeoutSec(timeoutSec); 192 } 193 194 195 public long getCountHandledConnections() { 196 return acceptor.getNumberOfHandledConnections(); 197 } 198 199 200 public int getDispatcherPoolSize() { 201 return acceptor.getDispatcherPoolSize(); 202 } 203 204 public void setDispatcherPoolSize(int size) { 205 acceptor.setDispatcherPoolSize(size); 206 } 207 208 209 public List <String > getOpenConnections() { 210 return acceptor.getOpenConnections(); 211 } 212 213 214 public int getNumberOpenConnections() { 215 return acceptor.getNumberOfOpenConnections(); 216 } 217 218 219 public long getCountConnectionTimeouts() { 220 return acceptor.getNumberOfConnectionTimeouts(); 221 } 222 223 public long getCountIdleTimeouts() { 224 return acceptor.getNumberOfIdleTimeouts(); 225 } 226 227 232 public int getLocalPort() { 233 return server.getLocalPort(); 234 } 235 236 237 241 public String getLocalAddressString() { 242 return server.getLocalAddress().toString(); 243 } 244 245 246 247 public Integer getWorkerpoolActiveCount() { 248 if (server.getWorkerpool() instanceof ThreadPoolExecutor ) { 249 ThreadPoolExecutor tpe = (ThreadPoolExecutor ) server.getWorkerpool(); 250 return tpe.getActiveCount(); 251 252 } else { 253 return null; 254 } 255 } 256 257 public Integer getWorkerpoolMaximumPoolSize() { 258 if (server.getWorkerpool() instanceof ThreadPoolExecutor ) { 259 ThreadPoolExecutor tpe = (ThreadPoolExecutor ) server.getWorkerpool(); 260 return tpe.getMaximumPoolSize(); 261 262 } else { 263 return null; 264 } 265 } 266 267 268 public Integer getWorkerpoolSize() { 269 if (server.getWorkerpool() instanceof ThreadPoolExecutor ) { 270 ThreadPoolExecutor tpe = (ThreadPoolExecutor ) server.getWorkerpool(); 271 return tpe.getPoolSize(); 272 273 } else { 274 return null; 275 } 276 } 277 278 public Integer getWorkerpoolLargestPoolSize() { 279 if (server.getWorkerpool() instanceof ThreadPoolExecutor ) { 280 ThreadPoolExecutor tpe = (ThreadPoolExecutor ) server.getWorkerpool(); 281 return tpe.getLargestPoolSize(); 282 283 } else { 284 return null; 285 } 286 } 287 288 public Integer getWorkerpoolKeepAliveTimeSec() { 289 if (server.getWorkerpool() instanceof ThreadPoolExecutor ) { 290 ThreadPoolExecutor tpe = (ThreadPoolExecutor ) server.getWorkerpool(); 291 return (int) tpe.getKeepAliveTime(TimeUnit.SECONDS); 292 293 } else { 294 return null; 295 } 296 } 297 } 298 299 } | Popular Tags |