RMI是Java的一组拥护开发分布式应用程序的API。RMI使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。
简单地说。这样使原先的程序在同一操作系统的方法调用,变成了不同操作系统之间程序的方法调用。因为J2EE是分布式程序平台,它以RMI机制实现程序组件在不同操作系统之间的通信。比方,一个EJB能够通过RMI调用Web上还有一台机器上的EJB远程方法。
用例server端结构大概如此
首先定义要传送的实体类。由于要网络传输,必须实现序列化
User.java
package com.yiyuwanglu.rmi.entity; import java.io.Serializable; public class User implements Serializable { /** * */ private static final long serialVersionUID = -8400949180923337013L; private String id; private String name; private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }第二定义实体接口,必须继承 java.rmi.Remote
UserService
package com.yiyuwanglu.rmi.service; import java.rmi.Remote; import java.rmi.RemoteException; import com.yiyuwanglu.rmi.entity.User; public interface UserService extends Remote { public User getUserById(String id)throws RemoteException; }第三。实现接口 记得继承UnicastRemoteObject
UserServiceImpl
package com.yiyuwanglu.rmi.service.impl; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import com.yiyuwanglu.rmi.entity.User; import com.yiyuwanglu.rmi.service.UserService; public class UserServiceImpl extends UnicastRemoteObject implements UserService { /** * */ private static final long serialVersionUID = 6222175854495075991L; public UserServiceImpl() throws RemoteException { super(); // TODO Auto-generated constructor stub } @Override public User getUserById(String id) { User user=new User(); user.setId(id); user.setAge(100); user.setName("測试"); return user; } }
server端启动
Program.java
package com.yiyuwanglu.rmi.program; import java.net.MalformedURLException; import java.nio.channels.AlreadyBoundException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import com.yiyuwanglu.rmi.service.UserService; import com.yiyuwanglu.rmi.service.impl.UserServiceImpl; public class Program{ public static void main(String[] args) { // TODO Auto-generated method stub try { UserService userService=new UserServiceImpl(); //注冊通讯端口 LocateRegistry.createRegistry(6600); //注冊通讯路径 Naming.rebind("rmi://127.0.0.1:6600/userService", userService); System.out.println("Service Start!"); } catch (RemoteException e) { System.out.println("创建远程对象发生异常!"); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println("发生反复绑定对象异常!"); e.printStackTrace(); } catch (MalformedURLException e) { System.out.println("发生URL畸形异常!"); e.printStackTrace(); } } }
client測试。最好找还有一台机子(当然同一台机子或许,只是为了模拟真实情况,还是还有一台机子。记得iP要换)创建client,切记:相关的类的包的路径要跟server一样
比如:client的 UserService
package com.yiyuwanglu.rmi.service;最后client调用
package com.yiyuwanglu.rmi.program; import java.rmi.Naming; import com.yiyuwanglu.rmi.entity.User; import com.yiyuwanglu.rmi.service.UserService; public class Client { public static void main(String[] args){ try{ //调用远程对象,注意RMI路径与接口必须与server配置一致 UserService userService=(UserService)Naming.lookup("rmi://127.0.0.1:6600/userService"); User user =userService.getUserById("1245"); System.out.println(user.getName()); }catch(Exception ex){ ex.printStackTrace(); } } }
先启动server端 Program 然后启动client Client显示结果各自是
Service Start!
client
測试