vendredi avril 28, 2006
If you are interested in modifying, recompiling and using the JMX source code part of Java SE 6 (aka Mustang), but do not want to bother with setting up the build of the whole Mustang sources, this "Hands On Lab" is for you. It will show you how to easily take advantage of the Ant's build.xml file provided with JMX sources to achieve just this. The level of this lab is beginner to intermediate.
/home/asmith/ant/1.6.5 /home/asmith/mustang_snapshot/b77_bin /home/asmith/mustang_snapshot/b77_src
Check the requirements for this lab listed above and make sure you have available on your system:
Add Ant and the Mustang binaries to your PATH environment variable, and then check this is correct, eg:
$ export PATH=/home/asmith/ant/1.6.5/bin:/home/asmith/mustang_snapshot/b77_bin/jdk1.6.0/bin:${PATH}
$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005
$ java -version
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b77)
Java HotSpot(TM) Server VM (build 1.6.0-beta2-b77, mixed mode)
cd into the j2se subdir of the directory where you have your extracted copy of the Mustang source snapshot.
Checks its contents are for now similar to what is shown below.
After we have performed the build, a build_jmx subdir will appear here.
$ cd /home/asmith/mustang_snapshot/b77_src/j2se $ ls -l drwxrwxr-x 14 asmith staff 512 Mar 30 17:17 make/ drwxrwxr-x 6 asmith staff 512 Mar 30 17:19 src/Note that we will ignore the contents of the
make subdir for this lab.
build.xml file is there.
$ cd src/share/classes/javax/management $ ls -l build.xml -rw-rw-r-- 1 asmith staff 9132 Mar 30 17:18 build.xml
ant -projecthelp.
Now, simply launch the build of JMX classes by typing:
$ ant
$ cd ../../../../../build_jmx/lib $ ls -l -rw-rw-r-- 1 asmith staff 8351568 Mar 30 17:52 jmx.jar $ unzip -p jmx.jar META-INF/MANIFEST.MF Manifest-Version: 1.0 Ant-Version: Apache Ant 1.6.5 Created-By: 1.6.0-beta2-b77 (Sun Microsystems Inc.) Build-JDK: 1.6.0-beta2-b77 Build-Platform: sparc SunOS 5.10 Build-User: asmith Name: common Sealed: true Specification-Title: JMX(TM) API Specification-Version: 1.3 Specification-Vendor: Sun Microsystems, Inc. Implementation-Title: JMX(TM) API, Java SE 6 implementation Implementation-Version: 2006.03.30_17:51:45_MEST rebuild of Mustang JMX sources Implementation-Vendor: Source bundle from Sun Microsystems, Inc. - Customer rebuilt
cd up to the j2se directory, the one containing the build_jmx subdir,
and create an example_jmx subdir into which to write and build our simple JMX agent:
$ cd ../../ $ pwd /home/asmith/mustang_snapshot/b77_src/j2se $ mkdir example_jmx
example_jmx subdir:
$ cd example_jmx
Agent.java file by copying the code below and saving it into a file named Agent.java:
/* Simple JMX Agent which prints the JMX implementation name */
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerDelegate;
public class Agent {
public static void main(String[] args) throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
String implName = (String) server.getAttribute(
MBeanServerDelegate.DELEGATE_NAME,"ImplementationName");
System.out.println("JMX Implementation Name = " + implName );
}
}
Agent.java file:
$ javac Agent.java
Agent.class file is there.
jmx.jar,
otherwise the JMX classes already in the Mustang binaries would take precedence over the built ones:
$ java -Xbootclasspath/p:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar Agent JMX Implementation Name = JMX
ServiceName.java file and rebuild JMX classes
cd into the JMX source directory containing the ServiceName.java file:
$ cd ../src/share/classes/com/sun/jmx/defaults/
ServiceName.java file and change the value of the JMX_IMPL_NAME string.
Let's modify it so that the line reads, say:
public static final String JMX_IMPL_NAME = "JMX_JavaOne";
build.xml file and rebuild all JMX classes:
$ cd ../../../../javax/management/ $ ant allNOTE: it is important above to clean the previously built classes (which the target "all" does) before building them again as the
JMX_IMPL_NAME is a static field and the javac compiler
inlines its value in the compiled ServiceName class and in all the classes using it.
cd back into the example_jmx subdir and run again the simple JMX Agent:
$ cd ../../../../../example_jmx/ $ java -Xbootclasspath/p:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar Agent JMX Implementation Name = JMX_JavaOneNOTE: In the command above, make sure the path to your freshly rebuilt
jmx.jar which you put after -Xbootclasspath/p:
is correct, otherwise you will see the JMX Implementation Name value unchanged!
If the path to your freshly rebuilt jmx.jar file is incorrect, or if you simply
put it in your classpath instead of prepending your bootclasspath with it, the
JMX classes already in the Mustang platform binaries are loaded in priority, and
therefore you do not see your changes. Try just putting your modified jmx.jar in your classpath:
$ java -cp .:/home/asmith/mustang_snapshot/b77_src/j2se/build_jmx/lib/jmx.jar Agent JMX Implementation Name = JMX
This is the end of this lab.
I hope you enjoyed it, learned usefull tips for you to reuse, and will now play for yourself with the JMX code.
For more JMX examples and tutorials, you should check Daniel's blog articles, starting with: Looking for JMX Overview, Examples, Tutorial, and more?
--
Thanks,
Joël Féraud
export ANT_OPTS=-Xmx512m
and it should work.
Posted by Lars Westergren on mai 26, 2006 at 12:31 PM CEST #
[javac] javac: invalid source release: 1.6
from ant when doing the test build, then you have exported
PATH=/home/yourname/jdk1.6.0/bin
but you must also remember to do
export JAVA_HOME=/home/yourname/jdk1.6.0
Posted by Lars Westergren on juin 25, 2006 at 05:53 PM CEST #
Posted by 199.198.254.100 on octobre 05, 2006 at 09:10 PM CEST #
Posted by Kathik on décembre 29, 2006 at 04:22 PM CET #
Posted by 81.255.98.253 on mars 26, 2007 at 05:05 PM CEST #
Posted by John Burris on juin 14, 2007 at 02:34 PM CEST #
Hi,
Please send me some code to get the details about
the session & threads and print it on screen or write to a file which is show into jconsole.
Posted by Vikash on mai 03, 2008 at 10:08 AM CEST #