5.3.6. RMI

5.3.6.1. 简介

RMI(Remote Method Invocation,远程方法调用)能够让在客户端Java虚拟机上的对象像调用本地对象一样调用服务端Java虚拟机中的对象上的方法。其中RMI标准实现是Java RMI,之外还有Weblogic RMI、Spring RMI等不同的实现。

RMI中比较重要的两个概念是Stub和Skeleton,Stub和Skeleton对同一套接口进行实现,其中Stub由Client端调用,并不进行真正的实现,而是和Server端通信。Skeleton是Server端,监听来自Stub的连接,根据Stub发送的数据进行真正的操作。

5.3.6.2. 调用步骤

  • 客户调用客户端辅助对象Stub上的方法
  • 客户端辅助对象Stub打包调用信息(变量,方法名),通过网络发送给服务端辅助对象Skeleton
  • 服务端辅助对象Skeleton将客户端辅助对象发送来的信息解包,找出真正被调用的方法以及该方法所在对象
  • 调用真正服务对象上的真正方法,并将结果返回给服务端辅助对象Skeleton
  • 服务端辅助对象将结果打包,发送给客户端辅助对象Stub
  • 客户端辅助对象将返回值解包,返回给调用者
  • 客户获得返回值

5.3.6.3. 样例

一份代码样例如下(来自《Enterprise JavaBeans》):

5.3.6.3.1. Person接口定义

  1. public interface Person {
  2. public int getAge() throws Throwable;
  3. public String getName() throws Throwable;
  4. }

5.3.6.3.2. 使用PersonServer实现Person

  1. public class PersonServer implements Person {
  2. private int age;
  3. private String name;
  4. public PersonServer(String name, int age) {
  5. this.age = age;
  6. this.name = name;
  7. }
  8. public int getAge() {
  9. return age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. }

5.3.6.3.3. 使用Person_Stub实现Person

  1. import java.io.ObjectOutputStream;
  2. import java.io.ObjectInputStream;
  3. import java.net.Socket;
  4. public class Person_Stub implements Person {
  5. private Socket socket;
  6. public Person_Stub() throws Throwable {
  7. // connect to skeleton
  8. socket = new Socket("computer_name", 9000);
  9. }
  10. public int getAge() throws Throwable {
  11. // pass method name to skeleton
  12. ObjectOutputStream outStream =
  13. new ObjectOutputStream(socket.getOutputStream());
  14. outStream.writeObject("age");
  15. outStream.flush();
  16. ObjectInputStream inStream =
  17. new ObjectInputStream(socket.getInputStream());
  18. return inStream.readInt();
  19. }
  20. public String getName() throws Throwable {
  21. // pass method name to skeleton
  22. ObjectOutputStream outStream =
  23. new ObjectOutputStream(socket.getOutputStream());
  24. outStream.writeObject("name");
  25. outStream.flush();
  26. ObjectInputStream inStream =
  27. new ObjectInputStream(socket.getInputStream());
  28. return (String)inStream.readObject();
  29. }
  30. }

5.3.6.3.4. Skeleton的实现

  1. import java.io.ObjectOutputStream;
  2. import java.io.ObjectInputStream;
  3. import java.net.Socket;
  4. import java.net.ServerSocket;
  5. public class Person_Skeleton extends Thread {
  6. private PersonServer myServer;
  7. public Person_Skeleton(PersonServer server) {
  8. // get reference of object server
  9. this.myServer = server;
  10. }
  11. public void run() {
  12. try {
  13. // new socket at port 9000
  14. ServerSocket serverSocket = new ServerSocket(9000);
  15. // accept stub's request
  16. Socket socket = serverSocket.accept();
  17. while (socket != null) {
  18. // get stub's request
  19. ObjectInputStream inStream =
  20. new ObjectInputStream(socket.getInputStream());
  21. String method = (String)inStream.readObject();
  22. // check method name
  23. if (method.equals("age")) {
  24. // execute object server's business method
  25. int age = myServer.getAge();
  26. ObjectOutputStream outStream =
  27. new ObjectOutputStream(socket.getOutputStream());
  28. // return result to stub
  29. outStream.writeInt(age);
  30. outStream.flush();
  31. }
  32. if(method.equals("name")) {
  33. // execute object server's business method
  34. String name = myServer.getName();
  35. ObjectOutputStream outStream =
  36. new ObjectOutputStream(socket.getOutputStream());
  37. // return result to stub
  38. outStream.writeObject(name);
  39. outStream.flush();
  40. }
  41. }
  42. } catch(Throwable t) {
  43. t.printStackTrace();
  44. System.exit(0);
  45. }
  46. }
  47. public static void main(String args []) {
  48. // new object server
  49. PersonServer person = new PersonServer("Richard", 34);
  50. Person_Skeleton skel = new Person_Skeleton(person);
  51. skel.start();
  52. }
  53. }

5.3.6.3.5. Client实现

  1. public class PersonClient {
  2. public static void main(String [] args) {
  3. try {
  4. Person person = new Person_Stub();
  5. int age = person.getAge();
  6. String name = person.getName();
  7. System.out.println(name + " is " + age + " years old");
  8. } catch(Throwable t) {
  9. t.printStackTrace();
  10. }
  11. }
  12. }

5.3.6.4. T3协议

T3协议是用于在WebLogic服务器和其他类型的Java程序之间传输信息的协议,是Weblogic对RMI规范的实现。简单来说,可以把T3视为暴露JDNI给用户调用的接口。

5.3.6.5. JRMP

Java远程方法协议(Java Remote Method Protocol,JRMP)是特定于Java技术的、用于查找和引用远程对象的协议。这是运行在Java远程方法调用(RMI)之下、TCP/IP之上的线路层协议。

JRMP是一个Java特有的、适用于Java之间远程调用的基于流的协议,要求客户端和服务器上都使用Java对象。