// 制作远程接口, 声明所有的方法都会抛出 RemoteException public interface MyRemote extends Remote{ // 确定变量和返回值是属于原语类型或可序列化类型 // String 类型就是可序列化的 public String sayHello() throws RemoteException; }
// 制作远程实现 public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{ private static final long serialVersionUID = 2494818195984623711L; protected MyRemoteImpl() throws RemoteException { super(); } @Override public String sayHello() throws RemoteException { return "every body say, xiao tang tang"; } public static void main(String[] args) { try { MyRemote service = new MyRemoteImpl(); Naming.rebind("hehe", service); } catch (Exception e) { e.printStackTrace(); } } }
public class MyRemoteClient { public void go() { try { MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/hehe"); String s = service.sayHello(); System.out.println(s); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { MyRemoteClient client = new MyRemoteClient(); client.go(); } }
// 创建远程服务接口(服务器接口) public interface CandyMachineRemote extends Remote{ int getCount() throws RemoteException; String getLocation() throws RemoteException; State getState() throws RemoteException; }
public class CandyMachineProxyServer { public static void main(String[] args) throws RemoteException { CandyMachine machine = new CandyMachine("chengdu", 50); try { Naming.rebind("machine", machine); } catch (MalformedURLException e) { e.printStackTrace(); } new Thread(new Runnable() { // 开启一个线程实现client循环投币 @Override public void run() { System.out.println("\n\n ====== 下面进入循环测试(中奖率为20%) ======"); for (int i = 0; i < 5; i++) { machine.insertQuarter(); machine.turnCrank(); System.out.println(machine); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();; } }
E:\bench-cluster\cloud-data-preprocess\designPattern\src>rmiregistry
E:\bench-cluster\cloud-data-preprocess\designPattern\src>java com.designpattern.chapter11_proxy.CandyMachineProxyServer
public class CandyMachineProxyMonitor { CandyMachineRemote remoteMachine; public CandyMachineProxyMonitor(CandyMachineRemote remoteMachine) { this.remoteMachine = remoteMachine; } public void report() { int counter = 0; try { while(true) { System.out.println("round" + (++counter)); System.out.println("machine.location = " + remoteMachine.getLocation()); System.out.println("machine.count = " + remoteMachine.getCount()); System.out.println("machine.state = " + remoteMachine.getState()); if(counter == 5) { break; } Thread.sleep(10000); } } catch (Exception e) { e.printStackTrace(); } } }
<pre name="code" class="java">//client public class CandyMachineProxyMonitorTest { public static void main(String[] args) throws RemoteException { CandyMachineRemote remoteMachine; try { remoteMachine = (CandyMachineRemote) Naming.lookup("rmi://127.0.0.1/machine"); CandyMachineProxyMonitor monitor = new CandyMachineProxyMonitor(remoteMachine); monitor.report(); } catch (MalformedURLException | NotBoundException e) { e.printStackTrace(); } } }
原文:http://blog.csdn.net/pacosonswjtu/article/details/50995456