Ai đó có thể đưa ra một ví dụ gọi lại RMI đơn giản của Hello World? Tôi đã cố gắng để nghiên cứu nó nhưng tôi không thể tìm thấy một trong những mà tôi hiểu. Tôi không hiểu những gì gọi lại là/không.Ví dụ gọi lại RMI đơn giản
Đây là hiện tại Hello World RMI của tôi nếu nó giúp ...
Interface
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Khách hàng
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();
}
}
}
server
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();
}
}
}
có lẽ chương 4 của luận án này: http: //www.scribd.com/doc/67084961/MasterArbeit sẽ giúp bạn hiểu các cuộc gọi lại bằng RMI –