看看:http://java.sun.com/docs/books/tutorial/rmi/很详细的1、Writing an RMI Server
A、Designing a Remote Interface
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException; public interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
} B、Implementing a Remote Interface package engine;import java.rmi.*;
import java.rmi.server.*;
import compute.*;public class ComputeEngine extends UnicastRemoteObject
implements Compute
{
public ComputeEngine() throws RemoteException {
super();
} public Object executeTask(Task t) {
return t.execute();
} public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "//host/Compute";
try {
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception: " +
e.getMessage());
e.printStackTrace();
}
}
}
2、Creating A Client Program
package client;import java.rmi.*;
import java.math.*;
import compute.*;public class ComputePi {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/Compute";
Compute comp = (Compute) Naming.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]));
BigDecimal pi = (BigDecimal) (comp.executeTask(task));
System.out.println(pi);
} catch (Exception e) {
System.err.println("ComputePi exception: " +
e.getMessage());
e.printStackTrace();
}
}
}
3、Compiling and Running the Example
A、Designing a Remote Interface
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException; public interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
} B、Implementing a Remote Interface package engine;import java.rmi.*;
import java.rmi.server.*;
import compute.*;public class ComputeEngine extends UnicastRemoteObject
implements Compute
{
public ComputeEngine() throws RemoteException {
super();
} public Object executeTask(Task t) {
return t.execute();
} public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "//host/Compute";
try {
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception: " +
e.getMessage());
e.printStackTrace();
}
}
}
2、Creating A Client Program
package client;import java.rmi.*;
import java.math.*;
import compute.*;public class ComputePi {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/Compute";
Compute comp = (Compute) Naming.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]));
BigDecimal pi = (BigDecimal) (comp.executeTask(task));
System.out.println(pi);
} catch (Exception e) {
System.err.println("ComputePi exception: " +
e.getMessage());
e.printStackTrace();
}
}
}
3、Compiling and Running the Example
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货