Spring框架学习[Spring RMI实现远程调用](二)
b) { this.cacheStub = cacheStub; } //设置当连接失败时,是否刷新RMI stub public void setRefreshStubOnConnectFailure(boolean refreshStubOnConnectFailure) { this.refreshStubOnConnectFailure = refreshStubOnConnectFailure; } //设置客户端socket工厂 public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) { this.registryClientSocketFactory = registryClientSocketFactory; } //Spring IoC容器回调方法,由子类RmiProxyFactoryBean回调方法调用 public void afterPropertiesSet() { //调用父类RemoteInvocationBasedAccessor的回调方法 super.afterPropertiesSet(); prepare(); } //初始化RMI客户端 public void prepare() throws RemoteLookupFailureException { //如果设置了启动时查找RMI stub if (this.lookupStubOnStartup) { //查找RMI stub Remote remoteObj = lookupStub(); if (logger.isDebugEnabled()) { //如果查找到的RMI stub是RmiInvocationHandler类型 if (remoteObj instanceof RmiInvocationHandler) { logger.debug("RMI stub [" + getServiceUrl() + "] is an RMI invoker"); } //如果获取到客户端配置的serviceInterface不为null else if (getServiceInterface() != null) { //判断客户端配置的serviceInterface是否是RMI stub实例 boolean isImpl = getServiceInterface().isInstance(remoteObj); logger.debug("Using service interface [" + getServiceInterface().getName() + "] for RMI stub [" + getServiceUrl() + "] - " + (!isImpl "not " : "") + "directly implemented"); } } //如果设置了缓存RMI stub,将缓存的stub设置为查找到的RMI stub if (this.cacheStub) { this.cachedStub = remoteObj; } } } //查找RMI stub protected Remote lookupStub() throws RemoteLookupFailureException { try { Remote stub = null; //如果设置了客户端socket工厂 if (this.registryClientSocketFactory != null) { //获取并解析客户端配置的serviceUrl URL url = new URL(null, getServiceUrl(), new DummyURLStreamHandler()); //获取客户端配置的serviceUrl协议 String protocol = url.getProtocol(); /
/如果客户端配置的serviceUrl中协议不为null且不是rmi if (protocol != null && !"rmi".equals(protocol)) { throw new MalformedURLException("Invalid URL scheme '" + protocol + "'"); } //获取客户端配置的serviceUrl中的主机地址 String host = url.getHost(); //获取客户端配置的serviceUrl中的端口 int port = url.getPort(); //获取客户端配置的serviceUrl中请求路径 String name = url.getPath(); //如果请求路径不为null,且请求路径以”/”开头,则去掉”/” if (name != null && name.startsWith("/")) { name = name.substring(1); } //根据客户端配置的serviceUrl信息和客户端socket工厂创建远程对 //象引用 Registry registry = LocateRegistry.getRegistry(host, port, this.registryClientSocketFactory); //通过远程对象引用查找指定RMI请求的RMI stub stub = registry.lookup(name); } //如果客户端配置的serviceUrl中协议为null或者是rmi else { //直接通过RMI标准API查找客户端配置的serviceUrl的RMI stub stub = Naming.lookup(getServiceUrl()); } if (logger.isDebugEnabled()) { logger.debug("Located RMI stub with URL [" + getServiceUrl() + "]"); } return stub; } //对查找RMI stub过程中异常处理 catch (MalformedURLException ex) { throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex); } catch (NotBoundException ex) { throw new RemoteLookupFailureException( "Could not find RMI service [" + getServiceUrl() + "] in RMI registry", ex); } catch (RemoteException ex) { throw new RemoteLookupFailureException("Lookup of RMI stub failed", ex); } } //获取RMI stub protected Remote getStub() throws RemoteLookupFailureException { //如果没有配置缓存RMI stub,或者设置了启动时查找RMI stub或当连接失败时 //不刷新RMI stub if (!this.cacheStub || (this.lookupStubOnStartup && !this.refreshStubOnConnectFailure)) { //如果缓存的RMI stub不为null,则直接返回,否则,查找RMI stub return (this.cachedStub != null this.cachedStub : lookupStub()); } //如果设置了缓存RMI stu