青岛科技 发表于 2013-2-1 12:17:17

java RMI

RMI入门
1.定义接口
package com.test.rmi;
 
import java.rmi.Remote;
import java.rmi.RemoteException;
 
public interface RMITest extends Remote {
long getPerfectTime()throws RemoteException;
 
}
 
2.实现接口,定义服务类

package com.test.rmi;
 
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
 
public class RMITestImp extends UnicastRemoteObject implements RMITest {
 

protected RMITestImp() throws RemoteException {
super();

}
 

public long getPerfectTime() throws RemoteException {

return System.currentTimeMillis();
}

public static void main(String[] args) {
if(System.getSecurityManager()==null){
System.out.println("security manager");
System.setSecurityManager(new SecurityManager());
}

try {//创建远程对象的一个或多个实例
RMITestImp rt =new RMITestImp();
System.out.println("hello");

Naming.rebind("Hello", rt);//把hello注册到RMI注册服务器上,命名为Hello 
System.out.println("bind OK");
} catch (Exception e) {

}
}
 
}
3.定义客户端类
页: [1]
查看完整版本: java RMI