service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
localhost:8082
RMI方法实现
https://blog.csdn.net/MAOZEXIJR/article/details/78841094
JMX学习笔记(四) JMX RMI
https://blog.csdn.net/java_huashan/article/details/36199603
rmiInfo
https://labs.portcullis.co.uk/tools/
我的JMX心得 -- Server端
https://blog.csdn.net/derekjiang/article/details/4531952
Hello.java
package rmi; import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote{ // 远程对象的接口必须拓展 java.rmi.Remote 接口 String sayHello() throws RemoteException; // 接口中所有方法必须抛出 RemoteException 异常, 因为远程调用缺乏可靠性,总是存在失败的可能。 }
Server.java
package rmi; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class Server implements Hello{ public Server(){super();}; //The implementation class Server implements the remote interface Hello, //providing an implementation for the remote method sayHello. The method sayHello does not need to declare that it throws any exception because the method implementation itself does not throw RemoteException nor does it throw any other checked exceptions. public String sayHello() { return "Hello, world"; } public static void main(String[] args){ try { // 创建本机 1099 端口上的RMI registry Registry registry = LocateRegistry.createRegistry(1099); // jdk 1.5 以后利用 UnicastRemoteObject 动态生成 Stub // jdk 1.5 之前需要使用 rmic 命令来创建 Server obj = new Server(); Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 1099); // 有些RMI示例代码会让接口实现类直接继承自 UnicastRemoteObject ,效果是一样的,参见 UnicastRemoteObject 的构造函数,或则在接口实现类构造函数中做这个工作也可以 // 将 Stub 绑定到RMI注册表中,方式多样,上文已经提过 registry.bind("Hello", stub); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
Client.java
package rmi; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry("localhost"); Hello hello = (Hello) registry.lookup( "Hello"); String ret = hello.sayHello(); System. out.println( ret); } catch (Exception e) { e.printStackTrace(); } } }
Groovy调用RMI方法
package rmi; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; Registry registry = LocateRegistry.getRegistry("localhost"); Hello hello = (Hello) registry.lookup( "Hello"); String ret = hello.sayHello(); log.info ( ret);
报错
问题原因:
异常说的很明确CardUserInfo这个对象无权限使用。因为服务端返回来的CardUserInfo这个对象和客户端的CardUserInfo不一致。RMI要求这两个类必须一直,包括包名和方法属性等。
解决方案: 将Hello类打包成jar文件,放在 SmartBearSoapUI-Pro-5.1.2inext
用 rmiInfo-0.3.jar 查看
MBean例子
Demo.java
package rmi; public class Demo implements DemoMBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String printHello() { return "Hello "+ name; } public String printHello(String whoName) { return "Hello " + whoName; } }
DemoMBean
package rmi; public interface DemoMBean { public String getName() throws Exception; public void setName(String name) throws Exception; public String printHello() throws Exception; public String printHello(String whoName) throws Exception; }
DemoServer
package rmi; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.management.MBeanServer; import javax.management.ObjectName; import java.lang.management.ManagementFactory; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL;
import com.sun.jdmk.comm.HtmlAdaptorServer; public class DemoServer implements Hello{ public DemoServer(){super();}; //The implementation class Server implements the remote interface Hello, //providing an implementation for the remote method sayHello. The method sayHello does not need to declare that it throws any exception because the method implementation itself does not throw RemoteException nor does it throw any other checked exceptions. public String sayHello() { return "Hello, world"; } public static void main(String[] args){ try { //create mbean server MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
ObjectName adapterName;
adapterName = new ObjectName("TestDemo" + ":name=" + "htmladapter");
adapter.setPort(8082);
adapter.start();
server.registerMBean(adapter, adapterName);
//create object name ObjectName objectName = new ObjectName("jmxBean:name=demo"); //create mbean and register mbean server.registerMBean(new Demo(), objectName); /** * JMXConnectorServer service */ //这句话非常重要,不能缺少!注册一个端口,绑定url后,客户端就可以使用rmi通过url方式来连接JMXConnectorServer Registry registry = LocateRegistry.createRegistry(1099); //构造JMXServiceURL JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"); //创建JMXConnectorServer JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, server); //启动 cs.start(); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
Groovy
package rmi; 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; HashMap<String, Object> prop = new HashMap<String, Object>(); prop.put(JMXConnector.CREDENTIALS, "DemoMBean"); 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("jmxBean:name=demo"); DemoMBean demo = JMX.newMBeanProxy(mbsc, mbeanName,DemoMBean.class); demo.setName("World!"); log.info demo.printHello();
Agent实现中为MBeanServer添加了一个htmladapter,这样我们就可以通过网页的方式来进行管理。
http://localhost:8082