001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jmx.support; 018 019import java.io.IOException; 020import java.util.HashMap; 021import java.util.Map; 022import java.util.Properties; 023import javax.management.JMException; 024import javax.management.MBeanServer; 025import javax.management.MalformedObjectNameException; 026import javax.management.ObjectName; 027import javax.management.remote.JMXConnectorServer; 028import javax.management.remote.JMXConnectorServerFactory; 029import javax.management.remote.JMXServiceURL; 030import javax.management.remote.MBeanServerForwarder; 031 032import org.springframework.beans.factory.DisposableBean; 033import org.springframework.beans.factory.FactoryBean; 034import org.springframework.beans.factory.InitializingBean; 035import org.springframework.jmx.JmxException; 036import org.springframework.util.CollectionUtils; 037 038/** 039 * {@link FactoryBean} that creates a JSR-160 {@link JMXConnectorServer}, 040 * optionally registers it with the {@link MBeanServer} and then starts it. 041 * 042 * <p>The {@code JMXConnectorServer} can be started in a separate thread by setting the 043 * {@code threaded} property to {@code true}. You can configure this thread to be a 044 * daemon thread by setting the {@code daemon} property to {@code true}. 045 * 046 * <p>The {@code JMXConnectorServer} is correctly shutdown when an instance of this 047 * class is destroyed on shutdown of the containing {@code ApplicationContext}. 048 * 049 * @author Rob Harrop 050 * @author Juergen Hoeller 051 * @since 1.2 052 * @see JMXConnectorServer 053 * @see MBeanServer 054 */ 055public class ConnectorServerFactoryBean extends MBeanRegistrationSupport 056 implements FactoryBean<JMXConnectorServer>, InitializingBean, DisposableBean { 057 058 /** The default service URL */ 059 public static final String DEFAULT_SERVICE_URL = "service:jmx:jmxmp://localhost:9875"; 060 061 062 private String serviceUrl = DEFAULT_SERVICE_URL; 063 064 private Map<String, Object> environment = new HashMap<String, Object>(); 065 066 private MBeanServerForwarder forwarder; 067 068 private ObjectName objectName; 069 070 private boolean threaded = false; 071 072 private boolean daemon = false; 073 074 private JMXConnectorServer connectorServer; 075 076 077 /** 078 * Set the service URL for the {@code JMXConnectorServer}. 079 */ 080 public void setServiceUrl(String serviceUrl) { 081 this.serviceUrl = serviceUrl; 082 } 083 084 /** 085 * Set the environment properties used to construct the {@code JMXConnectorServer} 086 * as {@code java.util.Properties} (String key/value pairs). 087 */ 088 public void setEnvironment(Properties environment) { 089 CollectionUtils.mergePropertiesIntoMap(environment, this.environment); 090 } 091 092 /** 093 * Set the environment properties used to construct the {@code JMXConnector} 094 * as a {@code Map} of String keys and arbitrary Object values. 095 */ 096 public void setEnvironmentMap(Map<String, ?> environment) { 097 if (environment != null) { 098 this.environment.putAll(environment); 099 } 100 } 101 102 /** 103 * Set an MBeanServerForwarder to be applied to the {@code JMXConnectorServer}. 104 */ 105 public void setForwarder(MBeanServerForwarder forwarder) { 106 this.forwarder = forwarder; 107 } 108 109 /** 110 * Set the {@code ObjectName} used to register the {@code JMXConnectorServer} 111 * itself with the {@code MBeanServer}, as {@code ObjectName} instance 112 * or as {@code String}. 113 * @throws MalformedObjectNameException if the {@code ObjectName} is malformed 114 */ 115 public void setObjectName(Object objectName) throws MalformedObjectNameException { 116 this.objectName = ObjectNameManager.getInstance(objectName); 117 } 118 119 /** 120 * Set whether the {@code JMXConnectorServer} should be started in a separate thread. 121 */ 122 public void setThreaded(boolean threaded) { 123 this.threaded = threaded; 124 } 125 126 /** 127 * Set whether any threads started for the {@code JMXConnectorServer} should be 128 * started as daemon threads. 129 */ 130 public void setDaemon(boolean daemon) { 131 this.daemon = daemon; 132 } 133 134 135 /** 136 * Start the connector server. If the {@code threaded} flag is set to {@code true}, 137 * the {@code JMXConnectorServer} will be started in a separate thread. 138 * If the {@code daemon} flag is set to {@code true}, that thread will be 139 * started as a daemon thread. 140 * @throws JMException if a problem occurred when registering the connector server 141 * with the {@code MBeanServer} 142 * @throws IOException if there is a problem starting the connector server 143 */ 144 @Override 145 public void afterPropertiesSet() throws JMException, IOException { 146 if (this.server == null) { 147 this.server = JmxUtils.locateMBeanServer(); 148 } 149 150 // Create the JMX service URL. 151 JMXServiceURL url = new JMXServiceURL(this.serviceUrl); 152 153 // Create the connector server now. 154 this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server); 155 156 // Set the given MBeanServerForwarder, if any. 157 if (this.forwarder != null) { 158 this.connectorServer.setMBeanServerForwarder(this.forwarder); 159 } 160 161 // Do we want to register the connector with the MBean server? 162 if (this.objectName != null) { 163 doRegister(this.connectorServer, this.objectName); 164 } 165 166 try { 167 if (this.threaded) { 168 // Start the connector server asynchronously (in a separate thread). 169 Thread connectorThread = new Thread() { 170 @Override 171 public void run() { 172 try { 173 connectorServer.start(); 174 } 175 catch (IOException ex) { 176 throw new JmxException("Could not start JMX connector server after delay", ex); 177 } 178 } 179 }; 180 181 connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]"); 182 connectorThread.setDaemon(this.daemon); 183 connectorThread.start(); 184 } 185 else { 186 // Start the connector server in the same thread. 187 this.connectorServer.start(); 188 } 189 190 if (logger.isInfoEnabled()) { 191 logger.info("JMX connector server started: " + this.connectorServer); 192 } 193 } 194 195 catch (IOException ex) { 196 // Unregister the connector server if startup failed. 197 unregisterBeans(); 198 throw ex; 199 } 200 } 201 202 203 @Override 204 public JMXConnectorServer getObject() { 205 return this.connectorServer; 206 } 207 208 @Override 209 public Class<? extends JMXConnectorServer> getObjectType() { 210 return (this.connectorServer != null ? this.connectorServer.getClass() : JMXConnectorServer.class); 211 } 212 213 @Override 214 public boolean isSingleton() { 215 return true; 216 } 217 218 219 /** 220 * Stop the {@code JMXConnectorServer} managed by an instance of this class. 221 * Automatically called on {@code ApplicationContext} shutdown. 222 * @throws IOException if there is an error stopping the connector server 223 */ 224 @Override 225 public void destroy() throws IOException { 226 if (logger.isInfoEnabled()) { 227 logger.info("Stopping JMX connector server: " + this.connectorServer); 228 } 229 try { 230 this.connectorServer.stop(); 231 } 232 finally { 233 unregisterBeans(); 234 } 235 } 236 237}