【发布时间】:2012-05-03 18:38:31
【问题描述】:
谁能给出一个简单的 RMI 回调示例 Hello World?我一直在尝试研究它,但我似乎无法找到我理解的一个。我不明白回调是什么/做什么。
如果有帮助,这是我当前的 Hello World RMI...
界面
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
客户
package example.hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client(){}
public static void main(String[] args){
String host = (args.length < 1) ? null : args[0];
try{
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
服务器
package example.hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server(){}
@Override
public String sayHello() {
System.out.println("responded!");
return "Hello, world!";
}
public static void main(String[] args) {
try{
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
【问题讨论】:
-
也许本文的第 4 章:scribd.com/doc/67084961/MasterArbeit 会帮助你理解 RMI 的回调