通过代理模式第一篇,我们已经有了RMI的基础知识,现在可以用RMI实现糖果机的远程代理了。远程服务是一个GumballMachine糖果机,具有一些属性和获得属性值得方法。客户端是GumballMonitor监视器,监视器和远程服务糖果机不在同一个JVM堆中。
实现代码如下:
服务端:
远程服务接口,客户端所需要调用的方法都应该在接口中声明:
public interface GumballMachineRemote extends Remote{ public String getLocation() throws RemoteException; public int getCount() throws RemoteException; public State getState() throws RemoteException; }
远程服务类:
public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote{ int count; String location; State state; protected GumballMachine(String location,int numberGumballs) throws RemoteException { super(); // TODO Auto-generated constructor stub this.location=location; this.count=numberGumballs; } @Override public String getLocation() { // TODO Auto-generated method stub return location; } @Override public int getCount() { // TODO Auto-generated method stub return count; } @Override public State getState() { // TODO Auto-generated method stub return state; } public void setCount(int count) { this.count = count; } public void setLocation(String location) { this.location = location; } public void setState(State state) { this.state = state; }
状态接口:
}
public interface State extends Serializable{ public void insertQuarter(); public void ejectQuarter(); public void turnCrank(); public void dispense(); } public class NoQuarterState implements State{ @Override public void insertQuarter() { // TODO Auto-generated method stub } @Override public void ejectQuarter() { // TODO Auto-generated method stub } @Override public void turnCrank() { // TODO Auto-generated method stub } @Override public void dispense() { // TODO Auto-generated method stub } }
远程服务端测试类:
public class GumballMachineTestDriver { public static void main(String [] args) { GumballMachine gumballMachine=null; int count; if(args.length<2) { System.out.println("GumballMachine <name> <inventory>"); System.exit(1); } count=Integer.parseInt(args[1]); try { gumballMachine=new GumballMachine(args[0],count); Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
客户端:
监视器类:
public class GumballMonitor { GumballMachineRemote machine; public GumballMonitor(GumballMachineRemote machine) { this.machine=machine; } public void report() { try { System.out.println("Gumball Machine: "+ machine.getLocation()); System.out.println("Current inventory: "+ machine.getCount() + " gumballs"); System.out.println("Current state: " + machine.getState()); }catch(RemoteException e) { e.printStackTrace(); } }
监视器测试类:
public class GumballMonitorTestDriver { public static void main(String [] args) { String location[]={ "rmi://127.0.0.1/gumballmachine", "rmi://127.0.0.1/gumballmachine", "rmi://127.0.0.1/gumballmachine" }; GumballMonitor[] monitor=new GumballMonitor[location.length]; for(int i=0;i<location.length;i++) { try { GumballMachineRemote machine= (GumballMachineRemote)Naming.lookup(location[i]); monitor[i]=new GumballMonitor(machine); }catch(Exception e) { e.printStackTrace(); } } for(int i=0;i<location.length;i++) { monitor[i].report(); } } }