JMX M-LET Service

1.What does M-LET service can do?

   M-Let service provide a mechanism of dynamic loading new MBeans without restarting your agent. Sounds like the things Dynamic MBean can do,huh? But there are some differences btw them. M-LET service can let agent use classes that are not in its original startup CLASSPATH, dynamic MBean can't do this.

 

2.What should be done to make this service available?

   Sun provide this service thro MLet class, it is easy for your application to use this service only by pre-adding MLet Mbean to your MBean server before your agent starts.

 

                                //add MLet service
		ObjectName mLetService=new ObjectName("Hello:name=MLet");
		server.createMBean("javax.management.loading.MLet", mLetService);

 

3.MLetMBean Architechture


 

As we can see above, MLet class extends URLClassLoader, so MLet is capable of adding new urls to its classpath or getResource from classpath, of course, it use URLClassLoader to do this. That is why it inherits from URLClassLoader.

 

And let's check MLetMBean interface to see what we have.

public abstract interface MLetMBean
{
  public abstract Set<Object> getMBeansFromURL(String paramString)
    throws ServiceNotFoundException;

  public abstract Set<Object> getMBeansFromURL(URL paramURL)
    throws ServiceNotFoundException;

  public abstract void addURL(URL paramURL);

  public abstract void addURL(String paramString)
    throws ServiceNotFoundException;

  public abstract URL[] getURLs();

  public abstract URL getResource(String paramString);

  public abstract InputStream getResourceAsStream(String paramString);

  public abstract Enumeration<URL> getResources(String paramString)
    throws IOException;

  public abstract String getLibraryDirectory();

  public abstract void setLibraryDirectory(String paramString);
}

 

As we can see from MLetMBean interface, addURL() & getResource() related methods are actually methods from URLClassLoader. So what we need to pay attention to here is only the getMBeansFromURL() method,this method has two overrided versions:

  public abstract Set<Object> getMBeansFromURL(String paramString)
    throws ServiceNotFoundException;

  public abstract Set<Object> getMBeansFromURL(URL paramURL)
    throws ServiceNotFoundException;

 

the parameter paramURL is referring to the url of  a M-LET file. M-Let file is a XML-like file that contains some attributes for MBeans which are about to load. The following is a sample of M-LET entry:


 

You will be familiar with this if you have ever experience with XML. Now I will walk you through some commonly used atrributes of the M-LET file:

  • CODE:specify the specific class that contains an MBean implementation, its value must contains the package name.
  • ARCHIVE:its value can be either a jar file or a jar file list, which should be enclosed in quotation mark(") and filenames must be comma separated.
  • NAME: actually the objectname used to register in a MBean server.
  • CODEBASE: you might notice that we dont provide the exactly path for ARCHIVE attribute but the jar file name-'mlet.jar', what if there are many mlet.jar exist in the classpath? This attaribute will help give it a specific url path for ARCHIVE attribute, if it is not specified, we assume that the jar file specified in ARCHIVE attribute is to be in the same directory as the M-LET file.

And now let's go back to our getMBeansFromURL() method, what it actually does is creating a new MBean from the info pre-defined in the M-Let file.

 
 

For point #3 using server.createMBean to create a new MBean. You might get confused about the server here. Where does the server come from? Do you forget our MBeanRegistration interface? Recall from MLetMBean Architechture, and we can see that MLET implements this interface. Knowing how MLET class get its MBean server? Yes, I am sure you do.

 

4.Concrete Example

   OK, after we learn enough theories, time for us to construct a concrete example. I will demostrate that how use M-LET service to load MBean dynamicly with M-Let file.

 

   First thing is to finish our MBean which is about to be loaded. Here I use the simple MBean-Standard MBean.

 

package MLetService;

public class MLetSample implements MLetSampleMBean{

	public void sayHelloToMLet() {
		System.out.println("Hello,MLet sample");
	}
	
}

 

 

package MLetService;

public interface MLetSampleMBean {
	public void sayHelloToMLet();
}

 

    And then let's use jar command to pack them in a jar: jar -cvf mlet.jar MLetService/MLetSample.class MLetService/MLetSampleMBean.class

 

    And then the M-Let file:

 

   The last, of course, will be our MLetAgent:

public class MLetAgent {

	public static void main(String[] args) throws Exception{
		MBeanServer server=MBeanServerFactory.createMBeanServer();
		//new a adaptor and register it in the server
		HtmlAdaptorServer adaptor=new HtmlAdaptorServer();
		adaptor.setPort(8888);
		ObjectName adaptorName=new ObjectName("Hello:name=adaptor");
		server.registerMBean(adaptor, adaptorName);
		
		//add MLet service
		ObjectName mLetService=new ObjectName("Hello:name=MLet");
		server.createMBean("javax.management.loading.MLet", mLetService);
		
		adaptor.start();
	}

}

 

 

   Let's start our MLetAgent and get access to http://localhost:8888 to load our MLetSample MBean.

 

   Note: There is one point you need to keep in mind, that is you have to ensure that the mlet.jar is not in your startup MLetAgent's classpath. Since what I want to show you is a more realistic case. Of course, it is fine with mlet.jar to be in the MLetAgent's classpath. It can work too.

 

   When you connect to our MLetAgent, you can see that we dont have MLetSample in hand:

 

 

 Go into the MLetMBean's view(clicking the name=MLet link), and input the url of mlet file:



 Click the button 'getMBeansFromURL' and back to our agent's view to check what we get:



 

   Can you see that? Our MLetSample shows up.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值