Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 by Cloudsoft Corp.
   * Copyright 2007 by Sun Microsystems, Inc.
   */
  package brooklyn.util.jmx.jmxrmi;
  
 import java.util.Map;
 
This exposes JMX support for going through firewalls by starting an RMI registry server on a well-known port.

This implementation DOES NOT support port-forwarding however. The same hostname used internally (specified in RMI_HOSTNAME_PROPERTY or autodetected by java) must also be addressable by the JMX client. This is due to how the property is used internally by java during the RMI registry re-direction.

If you require that the client connects to a different hostname/IP than the one where the service is bound, consider using the Brooklyn JmxmpAgent, as this will not work!

This listens on RMI_REGISTRY_PORT_PROPERTY unless overridden by system property RMI_REGISTRY_PORT_PROPERTY ( RMI_REGISTRY_PORT_PROPERTY).

See also:
brooklyn.util.jmx.jmxmp.JmxmpAgent
https://blogs.oracle.com/jmxetc/entry/connecting_through_firewall_using_jmx
https://blogs.oracle.com/jmxetc/entry/more_on_premain_and_jmx
 
 public class JmxRmiAgent {

    
Port for RMI registry to listen on. Default to RMI_REGISTRY_DEFAULT_PORT.
 
     public static final String RMI_REGISTRY_PORT_PROPERTY = "brooklyn.jmx-agent.rmi-port";
     public static final String RMI_REGISTRY_DEFAULT_PORT = "9001";

    
Port for JMX server (sometimes called JMX_RMI server) to listen on. Default to JMX_SERVER_DEFAULT_PORT.
 
     public static final String JMX_SERVER_PORT_PROPERTY = "brooklyn.jmx-agent.jmx-port";
     public static final String JMX_SERVER_DEFAULT_PORT = "11099";

    
Hostname to advertise, and if JMX_SERVER_ADDRESS_WILDCARD_PROPERTY is false also the hostname/interface to bind to. Should never be 0.0.0.0 as it is publicly advertised.
 
     public static final String RMI_HOSTNAME_PROPERTY = "java.rmi.server.hostname";

    
Whether JMX should bind to all interfaces.
 
     public static final String JMX_SERVER_ADDRESS_WILDCARD_PROPERTY = "jmx.remote.server.address.wildcard";

    
The entry point, uses the JDK dynamic agent loading feature.
 
     public static void premain(String agentArgsthrows IOException {
         new JmxRmiAgent().startServer(System.getProperties());
     }
 
     public JMXConnectorServer startServer(Properties properties) {
         try {
             // Ensure cryptographically strong random number generator used
             // to choose the object number - see java.rmi.server.ObjID
             System.setProperty("java.rmi.server.randomIDs""true");
 
             // Start an RMI registry on port specified
             final int rmiPort = Integer.parseInt(System.getProperty());
             final int jmxPort = Integer.parseInt(System.getProperty());
             final String hostname = getLocalhostHostname(properties);
             
             ..println("Setting up JmxRmiAgent for: "+hostname+" "+rmiPort+" / "+jmxPort);
             
             LocateRegistry.createRegistry(rmiPort);
 
             // Retrieve the PlatformMBeanServer.
             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 
             // Environment map.
             Map<StringObjectenv = new LinkedHashMap<StringObject>();
             propagate(propertiesenv"true");
 
             // TODO Security
 
             // Create an RMI connector server.
             JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostname + ":" + jmxPort + "/jndi/rmi://" + hostname + ":" + rmiPort + "/jmxrmi");
 
             // Now create the server from the JMXServiceURL
             JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(urlenvmbs);
 
             // Start the RMI connector server.
             connector.start();
             ..println("JmxRmiAgent JMXConnectorServer active at: " + url);
 
             return connector;
         } catch (RuntimeException e) {
            ..println("Unable to start JMXConnectorServer: " + e);
            throw e;
        } catch (Exception e) {
            ..println("Unable to start JMXConnectorServer: " + e);
            throw new RuntimeException(e);
        }
    }

    
Copies the value of key from the source to the target, if set. Otherwise sets the defaultValueIfNotNull if that is not null.

Returns:
whether anything is set
    private static boolean propagate(Properties sourceMap<StringObjecttargetString keyObject defaultValueIfNotNull) {
        Object v = source.getProperty(key);
        if (v == nullv = defaultValueIfNotNull;
        if (v == nullreturn false;
        target.put(keyv);
        return true;
    }
    private String getLocalhostHostname(Properties propertiesthrows UnknownHostException {
        String hostname = properties == null ? null : properties.getProperty();
        if ("0.0.0.0".equals(hostname)) {
            ..println("WARN: invalid hostname 0.0.0.0 specified for JmxRmiAgent; " +
            		"it typically must be an address or hostname which is bindable on the machine where " +
            		"this service is running AND accessible by a client machine (access will likely be impossible)");
        }
        if (hostname == null || hostname.isEmpty()) {
            hostname = InetAddress.getLocalHost().getHostName();
        }
        return hostname;
    }

    
Convenience main method.
    public static void main(String[] argsthrows Exception {
        premain("");
    }
New to GrepCode? Check out our FAQ X