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

2014-11-24 03:00:32 · 作者: · 浏览: 2
b,且设置了启动时查找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 handleRemoteConnectFailure(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 do
Invoke(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 (InvocationTargetException ex) { Throwable exToThrow = ex.getTargetException(); RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow); throw exToThrow; } catch (Throwable ex) { throw new RemoteInvocationFailureException("Invocation of method [" + invocation.getMethod() + "] failed in RMI service [" + getServiceUrl() + "]", ex); } } //如果RMI stub不是RmiInvocationHandler类型 else { //使用传统的RMI调用方式 try { return RmiClientInterceptorUtils.invokeRemoteMethod(invocation, stub); } catch (InvocationTargetException ex) { Throwable targetEx = ex.getTargetException(); if (targetEx instanceof RemoteException) { RemoteException rex = (RemoteException) targetEx; throw RmiClientInterceptorUtils.convertRmiAccessException( invocation.getMethod(), rex, isConnectFailure(rex), getServiceUrl()); } else { throw targetEx; } } } } //调用RmiInvocationHandler的RMI protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler) throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { //如果客户端远程调用请求是toString()方法 if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { return "RMI invoker proxy for service URL [" + getServiceUrl() + "]"; } //使用RmiInvocationHandler处理RMI调用 return invocationHandler.invoke(createRemoteInvocation(meth