1 16 17 package org.springframework.jmx.support; 18 19 import java.io.IOException ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import javax.management.JMException ; 24 import javax.management.MBeanServer ; 25 import javax.management.MalformedObjectNameException ; 26 import javax.management.ObjectName ; 27 import javax.management.remote.JMXConnectorServer ; 28 import javax.management.remote.JMXConnectorServerFactory ; 29 import javax.management.remote.JMXServiceURL ; 30 31 import org.springframework.beans.factory.DisposableBean; 32 import org.springframework.beans.factory.FactoryBean; 33 import org.springframework.beans.factory.InitializingBean; 34 import org.springframework.jmx.JmxException; 35 36 54 public class ConnectorServerFactoryBean extends MBeanRegistrationSupport 55 implements FactoryBean, InitializingBean, DisposableBean { 56 57 58 public static final String DEFAULT_SERVICE_URL = "service:jmx:jmxmp://localhost:9875"; 59 60 61 private String serviceUrl = DEFAULT_SERVICE_URL; 62 63 private Map environment; 64 65 private ObjectName objectName; 66 67 private boolean threaded = false; 68 69 private boolean daemon = false; 70 71 private JMXConnectorServer connectorServer; 72 73 74 77 public void setServiceUrl(String serviceUrl) { 78 this.serviceUrl = serviceUrl; 79 } 80 81 85 public void setEnvironment(Properties environment) { 86 this.environment = environment; 87 } 88 89 93 public void setEnvironmentMap(Map environment) { 94 this.environment = environment; 95 } 96 97 103 public void setObjectName(Object objectName) throws MalformedObjectNameException { 104 this.objectName = ObjectNameManager.getInstance(objectName); 105 } 106 107 110 public void setThreaded(boolean threaded) { 111 this.threaded = threaded; 112 } 113 114 118 public void setDaemon(boolean daemon) { 119 this.daemon = daemon; 120 } 121 122 123 132 public void afterPropertiesSet() throws JMException , IOException { 133 if (this.server == null) { 134 this.server = JmxUtils.locateMBeanServer(); 135 } 136 137 JMXServiceURL url = new JMXServiceURL (this.serviceUrl); 139 140 this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server); 142 143 if (this.objectName != null) { 145 doRegister(this.connectorServer, this.objectName); 146 } 147 148 try { 149 if (this.threaded) { 150 Thread connectorThread = new Thread () { 152 public void run() { 153 try { 154 connectorServer.start(); 155 } 156 catch (IOException ex) { 157 throw new JmxException("Could not start JMX connector server after delay", ex); 158 } 159 } 160 }; 161 162 connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]"); 163 connectorThread.setDaemon(this.daemon); 164 connectorThread.start(); 165 } 166 else { 167 this.connectorServer.start(); 169 } 170 171 if (logger.isInfoEnabled()) { 172 logger.info("JMX connector server started: " + this.connectorServer); 173 } 174 } 175 176 catch (IOException ex) { 177 unregisterBeans(); 179 throw ex; 180 } 181 } 182 183 184 public Object getObject() { 185 return this.connectorServer; 186 } 187 188 public Class getObjectType() { 189 return (this.connectorServer != null ? this.connectorServer.getClass() : JMXConnectorServer .class); 190 } 191 192 public boolean isSingleton() { 193 return true; 194 } 195 196 197 202 public void destroy() throws IOException { 203 if (logger.isInfoEnabled()) { 204 logger.info("Stopping JMX connector server: " + this.connectorServer); 205 } 206 try { 207 this.connectorServer.stop(); 208 } 209 finally { 210 unregisterBeans(); 211 } 212 } 213 214 } 215 | Popular Tags |