RMI 学习心得:
一.MS-DOS 下调试
1. 创建文件:
(1) 接口
package test.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloInterface extends Remote {
//rmi 应用程序必须继承自 java.rmi.Remote
String sayHello() throws RemoteException;
//定义可以远程调用的接口
}
(2) 服务
package test.rmi;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloService extends UnicastRemoteObject implements HelloInterface {
private String name;
public HelloService(String s) throws java.rmi.RemoteException {
super(); // 调用父类的构造函数
name = s;
}
public String sayHello() throws RemoteException {
return "Hello world!"; // 实现 Hello 接口定义的方法 ;
}
public static void main(String args[]) {
System.setSecurityManager(new RMISecurityManager()); // 设置 RMI 程序需要的安
全策略
try {
HelloService obj = new HelloService("HelloServer");
// 生成一个 HelloImpl 的实例
Naming.rebind("HelloServer", obj);
// 将这个实例绑定到一个名字上
评论0