Spring框架学习[Spring RMI实现远程调用](五)

2014-11-24 03:00:32 · 作者: · 浏览: 4
erviceUrl中协议为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 stub,且设置了启动时查找RMI stub或者当连接失败时刷新 //RMI stub else { //线程同步 synchronized (this.stubMonitor) { //如果缓存的RMI stub为null if (this.cachedStub == null) { //则将查找的RMI stub缓存 this.cachedStub = lookupStub(); } //返回缓存的RMI stub return this.cachedStub; } } } //拦截器对客户端远程调用方法的拦截入口 public Object invoke(MethodInvocation invocation) throws Throwable { //获取RMI stub Remote stub = getStub(); try { //拦截客户端远程调用方法 return doInvoke(invocation, stub); } catch (RemoteConnectFailureException ex) { return handleRem
oteConnectFailure(invocation, ex); } catch (RemoteException ex) { if (isConnectFailure(ex)) { return handleRemoteConnectFailure(invocation, ex); } else { throw ex; } } } //判断是否连接失败 protected boolean isConnectFailure(RemoteException ex) { return RmiClientInterceptorUtils.isConnectFailure(ex); } //处理远程连接失败 private Object handleRemoteConnectFailure(MethodInvocation invocation, Exception ex) throws Throwable { //如果设置了当连接失败时,刷新RMI stub if (this.refreshStubOnConnectFailure) { String msg = "Could not connect to RMI service [" + getServiceUrl() + "] - retrying"; if (logger.isDebugEnabled()) { logger.warn(msg, ex); } else if (logger.isWarnEnabled()) { logger.warn(msg); } //刷新查找远程调用stub return refreshAndRetry(invocation); } else { throw ex; } } //刷新RMI stub protected Object refreshAndRetry(MethodInvocation invocation) throws Throwable { Remote freshStub = null; //线程同步 synchronized (this.stubMonitor) { this.cachedStub = null; //查找RMI stub freshStub = lookupStub(); //如果设置了缓存RMI stub if (this.cacheStub) { //将刷新查找的RMI stub缓存 this.cachedStub = freshStub; } } return doInvoke(invocation, freshStub); } //具体RMI调用的地方 protected Object doInvoke(MethodInvocation invocation, Remote stub) throws Throwable { //如果RMI stub是RmiInvocationHandler类型 if (stub instanceof RmiInvocationHandler) { //调用RmiInvocationHandler的RMI try { return doInvoke(invocation, (RmiInvocationHandler) stub); } catch (RemoteException ex) { throw RmiClientInterceptorUtils.convertRmiAccessException( invocation.getMethod(), ex, isConnectFailure(ex), getServiceUrl()); } catch (InvocationTarget