Rmi远程调用机制
来源:互联网 发布:数据挖掘 面试 编辑:程序博客网 时间:2024/06/05 19:07
RMI是Java的一组拥护开发分布式应用程序的API。RMI使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。简单地说,这样使原先的程序在同一操作系统的方法调用,变成了不同操作系统之间程序的方法调用,由于J2EE是分布式程序平台,它以RMI机制实现程序组件在不同操作系统之间的通信。比如,一个EJB可以通过RMI调用Web上另一台机器上的EJB远程方法。
下图为rmi的结构调用示意:
下面给出一个简单的RMI 应用,其中类图如下:其中IService接口用于声明服务器端必须提供的服务(即service()方法),ServiceImpl类是具体的服务实现类,而Server类是最终负责注册服务器远程对象,以便在服务器端存在骨架代理对象来对客户端的请求提供处理和响应。
各个类源码实现为:
IService:
package com.base;import java.rmi.Remote;import java.rmi.RemoteException;/** *@author wanghjbuf *@date 2017年4月21日下午5:02:48 *@version 1.0 *@parameter */public interface IService extends Remote{public String service(String content) throws RemoteException;}
IServiceImpl:
package com.base;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;/** *@author wanghjbuf *@date 2017年4月21日下午5:04:22 *@version 1.0 *@parameter */@SuppressWarnings("serial")public class IServiceImpl extends UnicastRemoteObject implements IService{@SuppressWarnings("unused")private String name;public IServiceImpl(String name) throws RemoteException{ this.name=name;}@Overridepublic String service(String content){return ">>>>:"+content;}}
Server:
package com.server;import java.rmi.registry.LocateRegistry;import javax.naming.Context;import javax.naming.InitialContext;import com.base.IService;import com.base.IServiceImpl;/** *@author wanghjbuf *@date 2017年4月21日下午5:10:55 *@version 1.0 *@parameter */public class Server {public static void main(String[] args) {try{IService server = new IServiceImpl("server");LocateRegistry.createRegistry(8888);Context namingContext = new InitialContext();namingContext.rebind("rmi://192.168.188.112:8888/server", server);}catch(Exception e){e.printStackTrace();}System.out.println("服务器注册一个远程服务器!");}}
Client:
package com.client;import javax.naming.Context;import javax.naming.InitialContext;import com.base.IService;/** *@author wanghjbuf *@date 2017年4月21日下午5:15:15 *@version 1.0 *@parameter */public class Client {@SuppressWarnings("rawtypes")public static void main(String[] args) {String url = "rmi://192.168.188.112:8888/";try{Context namingContext = new InitialContext();IService client = (IService)namingContext.lookup(url+"server"); Class clientClass = client.getClass(); System.out.println("server:"+clientClass.getName()); Class[] interfaces = clientClass.getInterfaces(); for(Class c : interfaces){ System.out.println("interface:"+c.getName()); } System.out.println(client.service("hello!"));}catch(Exception e){e.printStackTrace();}}}
输出结果如下:
服务端:
客户端:
1 0
- Rmi远程调用机制
- 远程方法调用(RMI)机制
- 远程方法调用(RMI)机制
- Java远程方法调用(RMI)机制
- RMI,远程方法调用
- RMI远程调用实例
- RMI远程调用
- 远程调用 - spring+rmi
- RMI远程方法调用
- Java rmi 远程调用
- RMI远程调用过程
- JAVA RMI 远程调用
- java--- RMI远程调用
- RMI远程调用
- Java rmi 远程调用
- RMI远程调用
- rmi 远程方法调用
- rmi远程调用
- NYPJ 蛇形填数
- 腾讯面经:错过校招的我如何在社招进入鹅厂
- 学习MVC之租房网站(五)-权限、角色、用户管理
- 绘制人物属性的六芒星
- 水晶报表中让行高自适应高度(即根据内容的来适应高度)
- Rmi远程调用机制
- Java设计模式《七》模板方法模式
- Linux下which、whereis、locate、find 命令的区别
- <a>标签不跳转的几种实现方式
- rabbitMq消费者角度:消息分发、消息应答(ACK)、公平分发
- 矩阵连乘
- 对【2017.4.22 B组NOIP提高模拟赛】总结
- mysql 远程登录错误总结
- DbAPPSec CTF一道题的分析