1,实现Remote 接口
public interface ExecuteTaskI extends Remote{
long getCurrentTime() throws RemoteException;}
2,
public class ExecuteTask extends UnicastRemoteObject implements ExecuteTaskI {
/**
* 序列化编号
*/
private static final long serialVersionUID = 5106682387234335126L; public ExecuteTask() throws RemoteException {
super();
} @Override
public long getCurrentTime() throws RemoteException {
System.out.println("successfully");
return System.currentTimeMillis();
}
}3,
public static void main(String[] args) throws AlreadyBoundException {
//System.setSecurityManager(new RMISecurityManager());
try {
ExecuteTask exec = new ExecuteTask();
LocateRegistry.createRegistry(8889);
Naming.rebind("//10.30.12.141:8889/Detop", exec);
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}4,
public static void main(String[] args) {
try {
String url = "//10.30.12.141:8889/Detop";
ExecuteTaskI RmiObject = (ExecuteTaskI) Naming.lookup(url);
System.out.println("获取当前时间 :" + RmiObject.getCurrentTime());
} 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());
} }