本文主要是介绍JMX RMI简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
连接器概念:
JMX规范定义了连接器的概念(Connectors),连接器位于JMX的3层构架中的分布式服务层。连接器负责建立MBean服务器和管理应用之间的通信。连接器由一个驻留在代理层的连接器服务器(connector server)和管理应用的连接器客户端(connector client)构成。连接器服务端(connector server)连接到MBean服务器并监听来自客户端(connector client)的请求。一个connector client负责与connector server建立连接。connnector client通常与connector server在不同的JVM里并且通常运行在不同的机器上。1.连接器类型(Connector type)
connnector client通过给定协议与connector server建立连接,远程访问MBean服务器。JMX API允许使用不同类型的连接器来连接MBean服务器。
* JMX API定义了一个标准连接器 - RMI Connector,它支持通过RMI协议远程访问一个MBeanServer
* JMX API定义了一个可选的连接器 - JMXMP Connector.它实现了JMXMP协议(JMX Message Protocol).作为一个可选连接器,它不集成在JavaSE平台中,若需要,从Sun官方 下载jar
* 用户自定义连接器协议
2.连接器服务器地址
通常,一个connector server有一个地址,由类JMXServiceURL表示,这个类的实例是不可变的。使用不同的连接器,会有不同的JMXServiceURL编写方式。
格式看起来如下:
service:jmx:protocol:sap
说明:
service:jmx: 这个是JMX URL的标准前缀,所有的JMX URL都必须以该字符串开头。
protocol是指定连接连接器服务器(connector server)的传输协议
方括号中的部分为可选部分
sap是连接器服务器的地址,sap的语法格式://[host[:port]][url-path]
host表示主机名,port是十进制的端口号
host和port可以省略,但没有host时,不能出现port
url-path 开始于一个"/"符号
参考JMX API : http://docs.Oracle.com/javase/8/docs/api/index.html?javax/management/remote/rmi/package-summary.html
参考:http://www.l99.com/EditText_view.action?textId=480750
3. RMI连接器
JMX API定义了一个标准连接器 - RMI Connector,它支持通过RMI远程访问一个MBeanServer
RMI连接器服务器的地址示例如下:
service:jmx:rmi://localhost:1099/jndi/rmi://localhost:8899/myname
说明:
在这个JMXServiceURL中,第一个rmi指的是rmi连接器,表示用连接器使用RMI传输协议,第二个rmi指定RMI注册RMI连服务接器存储存根
localhost:1099: 这个是connector server的IP和端口,该部分是一个可选项,可以被省略掉。如果省略的话,则connector server会随机任意选择一个可用的端口
/jndi/rmi://localhost:8899/myname: 这个是connector server的路径,表示Connector server的stud是使用JNDI API绑定在rmi://localhost:8899/myname这个地址上
创建一个RMI connector server
通常是提供一个RMI connector server的连接器地址,用JMXConnectorServerFactory.newJMXConnectorServer方法来创建RMI connector cerver
参考JMX API:http://docs.oracle.com/javase/8/docs/api/index.html?javax/management/remote/rmi/package-summary.html
下面是使用RMI连接器远程管理程序的例子:
Step 1.MBean接口定义
- package com.jmx.demo9;
- import java.io.Serializable;
- public interface HelloMBean extends Serializable {
- public void setName(String name);
- public String getName();
- public void sayHello();
- }
package com.jmx.demo9;import java.io.Serializable;public interface HelloMBean extends Serializable {public void setName(String name);public String getName();public void sayHello();}
Step 2.MBean接口实现类
- package com.jmx.demo9;
- import java.io.Serializable;
- public class Hello implements HelloMBean, Serializable {
- private String name;
- public synchronized void setName(String name) {
- this.name = name;
- }
- public synchronized String getName() {
- return name;
- }
- public synchronized void sayHello() {
- System.out.println("Hello," + name);
- }
- }
package com.jmx.demo9;import java.io.Serializable;public class Hello implements HelloMBean, Serializable {private String name;public synchronized void setName(String name) {this.name = name;}public synchronized String getName() {return name;}public synchronized void sayHello() {System.out.println("Hello," + name);}}
Step 3.服务端
- package com.jmx.demo9;
- import java.rmi.registry.LocateRegistry;
- import java.util.HashMap;
- import javax.management.MBeanServer;
- import javax.management.MBeanServerFactory;
- import javax.management.ObjectInstance;
- import javax.management.ObjectName;
- import javax.management.remote.JMXAuthenticator;
- import javax.management.remote.JMXConnectorServer;
- import javax.management.remote.JMXConnectorServerFactory;
- import javax.management.remote.JMXServiceURL;
- import javax.security.auth.Subject;
- public class JMXAgent {
- /**
- * @param args
- * the command line arguments
- */
- public static void main(String[] args) throws Exception {
- System.out.println("--------------JMX Agent----------- ");
- LocateRegistry.createRegistry(1099);
- MBeanServer server = MBeanServerFactory.createMBeanServer();
- ObjectName helloName = new ObjectName("com.jmx.demo9:name=Hello");
- Hello hello = new Hello();
- HashMap<String, Object> prop = new HashMap<String, Object>();
- prop.put(JMXConnectorServer.AUTHENTICATOR, new JMXAuthenticator() {
- public Subject authenticate(Object credentials) {
- if (credentials instanceof String) {
- if (credentials.equals("Hello")) {
- return new Subject();
- }
- }
- throw new SecurityException("not authicated");
- }
- });
- JMXConnectorServer cserver = JMXConnectorServerFactory
- .newJMXConnectorServer(new JMXServiceURL(
- "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"),
- prop, server);
- cserver.start();
- server.registerMBean(hello, helloName);
- for (ObjectInstance object : server.queryMBeans(null, null)) {
- System.out.println(object.getObjectName());
- }
- System.out.println(hello);
- System.out.println("start.....");
- System.out.println("\n");
- }
- }
package com.jmx.demo9;import java.rmi.registry.LocateRegistry;
import java.util.HashMap;import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXAuthenticator;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.security.auth.Subject;public class JMXAgent {/*** @param args* the command line arguments*/public static void main(String[] args) throws Exception {System.out.println("--------------JMX Agent----------- ");LocateRegistry.createRegistry(1099);MBeanServer server = MBeanServerFactory.createMBeanServer();ObjectName helloName = new ObjectName("com.jmx.demo9:name=Hello");Hello hello = new Hello();HashMap<String, Object> prop = new HashMap<String, Object>();prop.put(JMXConnectorServer.AUTHENTICATOR, new JMXAuthenticator() {public Subject authenticate(Object credentials) {if (credentials instanceof String) {if (credentials.equals("Hello")) {return new Subject();}}throw new SecurityException("not authicated");}});JMXConnectorServer cserver = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"),prop, server);cserver.start();server.registerMBean(hello, helloName);for (ObjectInstance object : server.queryMBeans(null, null)) {System.out.println(object.getObjectName());}System.out.println(hello);System.out.println("start.....");System.out.println("\n");}
}
note:ObjectName对象标识符命名规则参考JMX API:http://docs.oracle.com/javase/8/docs/api/javax/management/ObjectName.html
Step 4.客户端(管理应用程序)
- package com.jmx.demo9;
- import java.util.HashMap;
- import javax.management.JMX;
- import javax.management.MBeanServerConnection;
- import javax.management.ObjectName;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
- public class Client {
- public static void main(String[] args) throws Exception {
- HashMap<String, Object> prop = new HashMap<String, Object>();
- prop.put(JMXConnector.CREDENTIALS, "Hello");
- JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
- JMXConnector conn = JMXConnectorFactory.connect(url, prop);
- MBeanServerConnection mbsc = conn.getMBeanServerConnection();
- ObjectName mbeanName = new ObjectName("com.jmx.demo9:name=Hello");
- HelloMBean hello = JMX.newMBeanProxy(mbsc, mbeanName,HelloMBean.class);
- hello.setName("World!");
- hello.sayHello();
- }
- }
package com.jmx.demo9;import java.util.HashMap;import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;public class Client {public static void main(String[] args) throws Exception {HashMap<String, Object> prop = new HashMap<String, Object>();prop.put(JMXConnector.CREDENTIALS, "Hello");JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");JMXConnector conn = JMXConnectorFactory.connect(url, prop);MBeanServerConnection mbsc = conn.getMBeanServerConnection();ObjectName mbeanName = new ObjectName("com.jmx.demo9:name=Hello");HelloMBean hello = JMX.newMBeanProxy(mbsc, mbeanName,HelloMBean.class);hello.setName("World!");hello.sayHello();}
}
测试结果:
先启动JMXAgent,在启动Client
- --------------JMX Agent-----------
- JMImplementation:type=MBeanServerDelegate
- com.jmx.demo9:name=Hello
- com.jmx.demo9.Hello@47eaec
- start.....
- Hello,world
--------------JMX Agent-----------
JMImplementation:type=MBeanServerDelegate
com.jmx.demo9:name=Hello
com.jmx.demo9.Hello@47eaec
start.....Hello,world
转载自:http://blog.csdn.net/java_huashan/article/details/36199603
这篇关于JMX RMI简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!