1.首先编写客户端和服务端以及算法的代码。代码如下:
(1)接口代码
package com.wonder.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface RMIOperate extends Remote {
public double add(double x,double y) throws RemoteException;
public double minus(double x,double y) throws RemoteException;
public double add(List l) throws RemoteException;
}
(2)接口的算法实现
package com.wonder.rmi.server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import com.wonder.rmi.RMIOperate;
public class OperateImpl extends UnicastRemoteObject implements RMIOperate {
public OperateImpl() throws RemoteException {
super();
}
/**
*
*/
private static final long serialVersionUID = 1L;
public double add(double x, double y) throws RemoteException {
double z=x+y;
return z;
}
public double minus(double x, double y) throws RemoteException {
double z=x-y;
return z;
}
public double add(List l) throws RemoteException {
return Double.parseDouble(String.valueOf((Double)l.get(0) + (Double)l.get(1)));
}
}
(3)server端程序
package com.wonder.rmi.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
OperateImpl o = new OperateImpl();
System.out.println("Server is Starting...");
LocateRegistry.createRegistry(7099); // 这个端口应该和绑定的端口一样,如果不指定,默认的端口是1099
Naming.rebind("rmi://192.168.71.214:7099/RMIOperate",o); // 这个端口和上面的端口一样
System.out.println("Waiting RMI client Invoke ...");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.客户端程序
package com.wonder.rmi.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;
import com.wonder.rmi.RMIOperate;
public class RMIClient {
/**
* @param args
*/
@SuppressWarnings({"unchecked","unchecked"})
public static void main(String[] args) {
String host = "192.168.71.214";
try {
// 调用方式1
Registry reg = null;
reg = LocateRegistry.getRegistry("192.168.71.214",7099);
RMIOperate rmiObj = (RMIOperate) reg.lookup("RMIOperate");
// 调用方式2
RMIOperate rmiObj1 = (RMIOperate) Naming.lookup("rmi://192.168.71.214:7099/RMIOperate"); //要查找远程服务器的端口
double x = 1;
double y = 2;
List l = new ArrayList(2);
l.add(0,Double.valueOf("123"));
l.add(1,Double.valueOf("456"));
double z = rmiObj.add(x,y);
System.out.println("add="+z);
double w = rmiObj.minus(x,y);
System.out.println("minus="+w);
double _a = rmiObj.add(l);
System.out.println("add list="+_a);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.生成stub
c:/> java weblogic.rmic com.wonder.rmi.server.OperateImpl
为了方便测试,打成jar包,放入到startWebLogic.cmd的路径中
4.如果是在代码中调用了LocateRegistry,则不用在命令提示符窗口启动rmregistry命令
5.设置weblogic的启动类,com.wonder.rmi.server.RMIServer。启动weblogic不出错误就可以了。
二。常见的问题
1.如果是出现连接拒绝的问题,检查rmi注册是否正常启动。并且绑定的地址以及端口正确。
2.如果出现找不到stub类的情况,请检查是否使用rmic生成了相应的stub类。另外还可以检查一下是否正确设置的端口。