Can I register my own MBean by JVM built-in MBeans?
Hi!
I'm looking for a way to run my own MBean with JVM, just like how the JVM built-in MBeans run in JVM (you don't have to call them in your java program, but only set the JVM parameter -Dcom.sun.management.jmxremote).
Is there a mechanism to register my own MBean by the built-in MBeans?
Thank you!
[328 byte] By [
dash_huana] at [2007-11-27 10:54:48]

# 1
I'm not sure I understand your question, but if you just want to register your MBeans that you have created in the platform MBean server, you do something like:
ManagementFactory.getPlatformMBeanServer().registerMBean(myMBean, myMBeanObjectName);
# 2
Thank you but that's not wha I'm looking for.
I know how to register MBean by coding like you did, however what I want to know is how to register it when JVM runs, In other words, I want my MBean to run when JVM starts, note that the JVM is running another java program, not my MBean.
To be more clearer, I want my MBean to run in they way like JVM built-in MBeans run:
1)They can run with any java program.
2)You don' t have to explicitly register them in your java code.
# 3
I can see two ways to accomplish what you are asking for.
The first is to define an "instrumentation agent" as described in the documentation for java.lang.instrument. You create myagent.jar which has a class com.example.PreMain with a method premain(String,Instrumentation) that registers your MBeans. You arrange for Premain-Class: com.example.PreMain to appear in the jar's manifest. Then you run your applications with -javaagent:myagent.jar.
The second is to intervene during the construction of MBeanServer instances by running your applications with -Djavax.management.builder.initial=com.example.MyMBeanServerBuilder. The MyMBeanServerBuilder class might look like this:
public class MyMBeanServerBuilder extends MBeanServerBuilder {
@Override
public MBeanServer newMBeanServer(
String defaultDomain, MBeanServer outer, MBeanServerDelegate delegate) {
MBeanServer mbs = super.newMBeanServer(defaultDomain, outer, delegate);
mbs.registerMBean(myMBean, myMBeanName);
return mbs;
}
}
I think the first approach is considerably cleaner.
Regards,
amonn McManus -- JMX Spec Lead -- http://weblogs.java.net/blog/emcmanus