远程代理模式-Remote Proxy(Java实现)
远程代理模式-Remote Proxy
服务端通过rmi将对象注册到远程服务, 客户端使用时, 只需要通过rmi协议获取即可, 只要接口统一, 即可不需要知道内部具体实现, 直接调用使用.
CompareHelper接口
这里就是客户端和服务端统一的接口, 只需要服务端根据这个接口实现相应的功能, 然后注册上去, 客户端就可以根据这个接口来使用相应的功能.
import java.rmi.RemoteException; import java.rmi.*; /** * 定义一个远程接口,必须继承Remote接口,其中需要远程调用的方法须抛出RemoteException异常 */ public interface CompareHelper<T> extends Remote { /** * 比较 object1 和 object2 的大小, * 如果object1大, 那么返回true * 如果object2大, 那么返回false */ boolean compare(T object1, T object2) throws RemoteException; /** * 将object2的值连接到object1的后面 */ T append(T object1, T object2) throws RemoteException; }
IntegerCompareHelper类
CompareHellper接口的其中一种实现. 用于处理Integer类型.
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class IntegerCompareHelper extends UnicastRemoteObject implements CompareHelper<Integer> { protected IntegerCompareHelper() throws RemoteException { } @Override public boolean compare(Integer object1, Integer object2) throws RemoteException { return object1 - object2 > 0; } @Override public Integer append(Integer object1, Integer object2) throws RemoteException { return Integer.valueOf(object1.toString() + object2.toString()); } }
StringCompareHelper类
CompareHellper接口的其中一种实现. 用于处理String类型.
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class StringCompareHelper extends UnicastRemoteObject implements CompareHelper<String> { protected StringCompareHelper() throws RemoteException { } @Override public boolean compare(String object1, String object2) throws RemoteException { return object1.compareTo(object2) > 0; } @Override public String append(String object1, String object2) throws RemoteException { return object1 + object2; } }
Server类
用于注册服务到远程服务上.
import java.net.MalformedURLException; import java.rmi.AlreadyBoundException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; /** * 创建RMI注册表,启动RMI服务,并将远程对象注册到RMI注册表中。 */ public class Server { private static final String HOST = "localhost"; private static final int PORT = 9090; public static void main(String args[]) { try { //创建2个对象, 准备将这个两个对象作为远程对象注册 CompareHelper stringCompareHelper = new StringCompareHelper(); CompareHelper integerCompareHelper = new IntegerCompareHelper(); LocateRegistry.createRegistry(PORT); //绑定的URL标准格式为:rmi://host:port/name(其中协议名可以省略,下面两种写法都是正确的) // 本例子中就是 "rmi://localhost:9090/XXXXXXCompareHelper" Naming.bind("rmi://" + HOST + ":" + PORT + "/StringCompareHelper", stringCompareHelper); Naming.bind("//" + HOST + ":" + PORT + "/IntegerCompareHelper", integerCompareHelper); System.out.println("---->远程对象绑定成功!"); } catch (RemoteException e) { System.out.println("创建远程对象发生异常!"); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println("发生重复绑定对象异常!"); e.printStackTrace(); } catch (MalformedURLException e) { System.out.println("发生URL畸形异常!"); e.printStackTrace(); } } }
Client类
用于调用远程服务.
import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; /** * 客户端测试,在客户端调用远程对象上的远程方法,并返回结果。 */ @SuppressWarnings("unchecked") public class Client { public static void main(String args[]) { try { CompareHelper compareHelper; compareHelper = (CompareHelper) Naming.lookup("rmi://localhost:9090/StringCompareHelper"); System.out.println(compareHelper.append("你好", "哈哈哈")); System.out.println(compareHelper.compare("abc", "ioi")); compareHelper = (CompareHelper) Naming.lookup("rmi://localhost:9090/IntegerCompareHelper"); System.out.println(compareHelper.append(6379, 8080)); System.out.println(compareHelper.compare(435, 666)); } catch (NotBoundException | MalformedURLException | RemoteException e) { e.printStackTrace(); } } }
如何运行呢?
先运行Server.java里的main方法来注册服务, 再运行Client.java里的main方法来进行相应服务的获取和调用.
---------------------------------------------------------
学如不及,犹恐失之
学如不及,犹恐失之