Welcome 微信登录

首页 / 软件开发 / JAVA / EJB的存根和骨架的工作原理

EJB的存根和骨架的工作原理2011-04-30一、RMI工作原理

RMI的本质就是实现在不同JVM之间的调用,它的实现方法就是在两个JVM中各开一个Stub和Skeleton,二者通过socket通信来实现参数和返回值的传递。

有关RMI的例子代码网上可以找到不少,但绝大部分都是通过extend the interface java.rmi.Remote实现,已经封装的很完善了,不免使人有雾里看花的感觉。下面的例子是我在《Enterprise JavaBeans》里看到的,虽然很粗糙,但很直观,利于很快了解它的工作原理。

1、定义一个Person的接口,其中有两个business method, getAge() 和getName()

代码:

public interface Person {
public int getAge() throws Throwable;
public String getName() throws Throwable;
}

2、Person的实现PersonServer类

代码:

public class PersonServer implements Person {
int age;
String name;
public PersonServer(String name, int age) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}