《MX4J学习文档.doc》由会员分享,可在线阅读,更多相关《MX4J学习文档.doc(8页珍藏版)》请在金锄头文库上搜索。
1、MX4JMade by Michael.Shimailto:MX4J3JMXServiceURL3JMXConnectorServer3例子:3AbstractDynamicMBean:5HttpAdaptor:6Constructor:6Attribute7XSLTProcessor7Brief Intro:7Constructor:7JMXAuthenticator7NamingService8部署8调用8更改端口8MX4JJMXServiceURL格式:service:jmx: :/ : / 它可以看做是JMXConnectorServer的地址,JMXConnector可以通过这个地址
2、连接到JMXConnectorServer。JMXConnectorServer一旦JMXConnectorServer执行stop方法,则不能restart.MX4J下JMX配置(利用HttpAdaptor和XSLTProcessor)这种配置功能上类似于HtmlAdaptor和JConnector,都是启动server,来进行MBean的注册。1creates MBeanServer2将MC4J HttpAdaptor 对象注册入MBean设HttpAdaptor MBean的attributes-port,host3将MC4J XSLTProcessor,注册入MBean设置一些attr
3、ibue,也可以不设置4加入被管理MBean(一些测试的bean)5执行HttpAdaptor MBean的具体服务方法 / 执行HttpAdaptor MBean的addAuthorization(username, password)方法 server.invoke(serverName, addAuthorization, new Objectmx4j, mx4j, new Stringjava.lang.String, java.lang.String); / 执行HttpAdaptor MBean(即serverName)的start()方 server.invoke(serverN
4、ame, start, null, null);例子:package com.primeton.spl.mx4j;import javax.management.Attribute;import javax.management.MBeanServer;import javax.management.MBeanServerFactory;import javax.management.ObjectName;import com.primeton.spl.dynamic.HelloDynamic;import mx4j.tools.adaptor.http.HttpAdaptor;import
5、mx4j.tools.adaptor.http.XSLTProcessor;public class HttpAdaptorsprivate String mbeanDomainName = MX4J;private int port = 8091;private String host = localhost;private String username = admin;private String password = admin;private String path = .;private String pathInJar = null;public void start() thr
6、ows Exception/ 开启MBeanServerMBeanServer mbServer = MBeanServerFactory.createMBeanServer(mbeanDomainName);/ 将MX4J 中的httpadpator注册到MBeanServer中ObjectName serverName = new ObjectName(Http:name=HttpAdaptor);HttpAdaptor adaptor = new HttpAdaptor();mbServer.registerMBean(adaptor, serverName);/ 设置httpadpat
7、or 属性mbServer.setAttribute(serverName, new Attribute(Port, port);mbServer.setAttribute(serverName, new Attribute(Host, host);/ MX4J要求 设置XSLTProcessor, 注入MBean/ 提供解析 XML功能ObjectName processorName = new ObjectName(Http:name=XSLTProcessor);XSLTProcessor xsltP = new XSLTProcessor();mbServer.registerMBea
8、n(xsltP, processorName);/ 设置XSLTProcessor属性mbServer.setAttribute(processorName, new Attribute(File, path);mbServer.setAttribute(processorName, new Attribute(UseCache, new Boolean(false);mbServer.setAttribute(processorName, new Attribute(PathInJar, pathInJar);/ 设置httpadaptor的ProcessormbServer.setAttr
9、ibute(serverName, new Attribute(ProcessorName, processorName);/ 添加business MBean/ HelloDynamic() 是一个MBean, 这里只是为了测试,可以换成其他的MBeanObjectName bzMBeanObjName = new ObjectName(bzMBean:name=Hello);mbServer.registerMBean(new HelloDynamic(), bzMBeanObjName);/ 执行 httpadaptor中的具体服务方法mbServer.invoke(serverName
10、, addAuthorization, new Stringusername, password, new StringString.class.getName(), String.class.getName();/ use basic authentication /server.setAttribute(serverName, new Attribute(AuthenticationMethod, basic);/ 执行httpadaptor 的start 方法mbServer.invoke(serverName, start, null, null);public static void
11、 main(String args) throws Exceptionnew HttpAdaptors().start();AbstractDynamicMBean:该工具类使得能够更加容易地写出Dynamic MBean。通过extends这个类,开发人员就不用implement DynamicMBean接口。只需要提供MBean的metadata(override few methods)和实现(implement methods)即可。经常被override的方法: createMBeanAttributeInfo, if the MBeans has manageable attrib
12、utes createMBeanOperationInfo, if the MBeans has manageable operations createMBeanNotificationInfo, if the MBeans has manageable notifications createMBeanConstructorInfo, if the MBeans has manageable constructors getMBeanDescription 通过override上述等方法,可以对属性、操作进行暴露。HttpAdaptormx4j.tools.adaptor.http.Htt
13、pAdaptor:该类设置basic adaptor 来监听HTTP请求(类似于HtmpAdaptor)。Constructor:有四种种构造方法:其中带参数的,可以分别设置port(int),host(String);利用无参构造方法可以在注册MBean后通过MBeanServer进行设置:HttpAdaptor httpAdaptor = new HttpAdaptor();ObjectName serverName = new ObjectName(DOMAIN_NAME + :name=HttpAdaptor);mbserver.registerMBean(httpAdaptor, s
14、erverName);mbserver.setAttribute(serverName, new Attribute(Host, host);addAuthorization(String username,String pwd)添加username和password,进行授权。start()开启服务器。注意:注册MBean后,都可以由MBean的invoke方法进行动态调用。AttributePort: 设置server监听端口,默认8080;Host:设置server监听主机名,默认localhost即你不能通过其他电脑进行访问。可以用0.0.0.0,这样对所有本地接口开放。AuthenticationMethod:设置认证方法,合法的是:none,basic,digest参考securit