rmi ---remote method invoke
head first 中的, 代理模式的试验:
准备:
1.三个类:
MyRemote.java
MyRemoteImpl.java
MyRemoteClient.java
MyRemote.java
import java.rmi.*;
public interface MyRemote extends Remote{
public String syaHello() throws RemoteException;
}
MyRemoteImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
public MyRemoteImpl() throws RemoteException
{
}
public String syaHello() {
return "Server syas hello!";
}
public static void main(String[] args){
try{
MyRemote service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
}catch (Exception e)
{
e.printStackTrace();
}
}
}
MyRemoteClient.java
import java.rmi.*;
public class MyRemoteClient {
public static void main(String[] args)
{
new MyRemoteClient().go();
}
private void go() {
try{
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/RemoteHello");
String s = service.syaHello();
System.out.println(s);
}catch(Exception e){
e.printStackTrace();
}
}
}
2.
编译以上三个文件, 也可从eclipes 文件目录中找到。
3.获取 客户端代理(桩), 和服务器代理(骨架) class 文件。
set classpath = .;%classpath%
rmic MyRemoteImpl
4. 开始 rmiregistry
start rmiregistry
5.开服务器端
set classpath = .;%classpath%
java MyRemoteImpl
6.开客户端
set classpath = .;%classpath%
java MyRemoteClient