Java RMI 远程过程调用,客户端和服务器端的实现
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package syh.com.rmi.test;
import java.rmi.Remote;import java.rmi.RemoteException;
public interface RmiSample extends Remote {
public int sum(int a, int b) throws RemoteException;
public String say() throws RemoteException;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package syh.com.rmi.test;
import java.rmi.Naming;
import java.rmi.RemoteException;
//import com.robin.demo.rmi.interf.RmiSample;
public class RmiSampleClient {
/**
* @param args
*/
public static void main(String[] args) {
try {
String url = "//localhost:8809/SAMPLE-SERVER";
RmiSample RmiObject = (RmiSample) Naming.lookup(url);
while(true){
System.out.println(" 1 + 2 = " + RmiObject.sum(1, 2));
System.out.println(RmiObject.say());
}
} catch (RemoteException rex) {
System.out.println("Error in lookup: " + rex.toString());
} catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString());
} catch (java.rmi.NotBoundException ne) {
System.out.println("NotBound: " + ne.toString());
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package syh.com.rmi.test;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
//import com.robin.demo.rmi.impl.RmiSampleImpl;
public class RmiSampleServer {
/**
* @param args
*/
public static void main(String[] args) {
try{
LocateRegistry.createRegistry(8809);
RmiSampleImpl server= new RmiSampleImpl("Hello,Shi Yan Hua!!!");
Naming.rebind("//localhost:8809/SAMPLE-SERVER" , server);
System.out.println("Hello Server is ready.");
}catch (MalformedURLException me){
System.out.println("Malformed URL: " + me.toString());
//System.out.println("Hello Server failed: " + e);
}catch(RemoteException re){
System.out.println("Remote Exception: "+re.toString());
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package syh.com.rmi.test;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
//import com.robin.demo.rmi.interf.RmiSample;
public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample {
/**
*
*/
private static final long serialVersionUID = 2742977636753958461L;
public RmiSampleImpl(String mes) throws RemoteException {
message = mes;
}
public int sum(int a, int b) throws RemoteException {
return a + b;
}
public String say() throws RemoteException {
System.out.println("Called by HelloClient");
return message;
}
private String message;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------