Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. /**
  2. * CustomAgent.java
  3.  
  4. * https://blogs.oracle.com/jmxetc/entry/connecting_through_firewall_using_jmx
  5. */
  6.  
  7. package example.rmi.agent;
  8.  
  9. import java.io.IOException;
  10. import java.lang.management.ManagementFactory;
  11. import java.net.InetAddress;
  12. import java.rmi.registry.LocateRegistry;
  13. import java.util.HashMap;
  14. import javax.management.MBeanServer;
  15. import javax.management.remote.JMXConnectorServer;
  16. import javax.management.remote.JMXConnectorServerFactory;
  17. import javax.management.remote.JMXServiceURL;
  18.  
  19. /**
  20. * This CustomAgent will start an RMI COnnector Server using only
  21. * port "example.rmi.agent.port".
  22. *
  23. * @author dfuchs
  24. */
  25. public class CustomAgent {
  26.  
  27. private CustomAgent() { }
  28.  
  29. public static void premain(String agentArgs)
  30. throws IOException {
  31.  
  32. // Ensure cryptographically strong random number generator used
  33. // to choose the object number - see java.rmi.server.ObjID
  34. //
  35. System.setProperty("java.rmi.server.randomIDs", "true");
  36.  
  37. // Start an RMI registry on port specified by example.rmi.agent.port
  38. // (default 3000).
  39. //
  40. final int port= Integer.parseInt(
  41. System.getProperty("example.rmi.agent.port","3000"));
  42. System.out.println("Create RMI registry on port "+port);
  43. LocateRegistry.createRegistry(port);
  44.  
  45.  
  46. // Retrieve the PlatformMBeanServer.
  47. //
  48. System.out.println("Get the platform's MBean server");
  49. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  50.  
  51. // Environment map.
  52. //
  53. System.out.println("Initialize the environment map");
  54. HashMap<String,Object> env = new HashMap<String,Object>();
  55.  
  56. // This where we would enable security - left out of this
  57. // for the sake of the example....
  58. //
  59.  
  60. // Create an RMI connector server.
  61. //
  62. // As specified in the JMXServiceURL the RMIServer stub will be
  63. // registered in the RMI registry running in the local host on
  64. // port 3000 with the name "jmxrmi". This is the same name the
  65. // out-of-the-box management agent uses to register the RMIServer
  66. // stub too.
  67. //
  68. // The port specified in "service:jmx:rmi://"+hostname+":"+port
  69. // is the second port, where RMI connection objects will be exported.
  70. // Here we use the same port as that we choose for the RMI registry.
  71. // The port for the RMI registry is specified in the second part
  72. // of the URL, in "rmi://"+hostname+":"+port
  73. //
  74. System.out.println("Create an RMI connector server");
  75. final String hostname = InetAddress.getLocalHost().getHostName();
  76. //JMXServiceURL url =
  77. // new JMXServiceURL("service:jmx:rmi://"+hostname+
  78. // ":"+port+"/jndi/rmi://"+hostname+":"+port+"/jmxrmi");
  79.  
  80. //Added to allow connection through a firewall
  81. // We use (port+1) to export the RMI connection objects
  82. JMXServiceURL url =
  83. new JMXServiceURL("service:jmx:rmi://"+hostname+
  84. ":"+(port+1)+"/jndi/rmi://"+hostname+":"+port+"/jmxrmi");
  85.  
  86.  
  87.  
  88.  
  89. // Now create the server from the JMXServiceURL
  90. //
  91. JMXConnectorServer cs =
  92. JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
  93.  
  94. // Start the RMI connector server.
  95. //
  96. System.out.println("Start the RMI connector server on port "+port);
  97. cs.start();
  98. }
  99. }
  100.  
  101. <project name="Agent" basedir="." default="main">
  102.  
  103. <property name="src.dir" value="src"/>
  104. <property name="build.dir" value="build"/>
  105. <property name="classes.dir" value="${build.dir}/classes"/>
  106. <property name="jar.dir" value="${build.dir}/jar"/>
  107. <property name="main-class" value="example.rmi.agent.CustomAgent"/>
  108.  
  109. <target name="clean">
  110. <delete dir="${build.dir}"/>
  111. </target>
  112.  
  113. <target name="compile">
  114. <mkdir dir="${classes.dir}"/>
  115. <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
  116. </target>
  117.  
  118. <target name="jar" depends="compile">
  119. <mkdir dir="${jar.dir}"/>
  120. <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
  121. <manifest>
  122. <attribute name="Main-Class" value="${main-class}"/>
  123. </manifest>
  124. </jar>
  125. </target>
  126.  
  127. <target name="run" depends="jar">
  128. <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
  129. </target>
  130. <target name="clean-build" depends="clean,jar"/>
  131. <target name="main" depends="clean,run"/>
  132.  
  133. <!-- Builds dist.agent.jar -->
  134. <target name="build-agent-jar" description="build an agent jar that can be used with -javaagent" depends="compile">
  135. <mkdir dir="${jar.dir}"/>
  136. <jar basedir="${classes.dir}" destfile="${jar.dir}/${ant.project.name}.jar">
  137. <manifest>
  138. <attribute name="Premain-Class" value="example.rmi.agent.CustomAgent"/>
  139. </manifest>
  140. </jar>
  141. <echo>To use this application with agent try:</echo>
  142. <echo>java -Dexample.rmi.port=3000 -javaagent:${dist.agent.jar} -classpath [application-classpath] [application-classpath] [application-main-class]</echo>
  143. </target>
  144. </project>
  145.  
  146. Dec 18, 2011 4:57:46 PM RMIConnector connect
  147. FINER: [javax.management.remote.rmi.RMIConnector: jmxServiceURL=service:jmx:rmi://localhost:6005/jndi/rmi://localhost:6004/jmxrmi]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement