Spring框架学习[Spring RMI实现远程调用](十)
2014-11-24 03:00:32
·
作者:
·
浏览: 11
); } //检查RMI注册的socket工厂 if (this.registryClientSocketFactory instanceof RMIServerSocketFactory) { this.registryServerSocketFactory = (RMIServerSocketFactory) this.registryClientSocketFactory; } if (this.registryClientSocketFactory == null && this.registryServerSocketFactory != null) { throw new IllegalArgumentException( "RMIServerSocketFactory without RMIClientSocketFactory for registry not supported"); } this.createdRegistry = false; //获取RMI注册器 if (this.registry == null) { this.registry = getRegistry(this.registryHost, this.registryPort, this.registryClientSocketFactory, this.registryServerSocketFactory); this.createdRegistry = true; } //获取要被导出的服务端远程对象 this.exportedObject = getObjectToExport(); if (logger.isInfoEnabled()) { logger.info("Binding service '" + this.serviceName + "' to RMI registry: " + this.registry); } //导出远程服务对象 if (this.clientSocketFactory != null) { UnicastRemoteObject.exportObject( this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory); } else { UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort); } //将RMI对象绑定到注册器 try { if (this.replaceExistingBinding) { this.registry.rebind(this.serviceName, this.exportedObject); } else { this.registry.bind(this.serviceName, this.exportedObject); } } //异常处理 catch (AlreadyBoundException ex) { unexportObjectSilently(); throw new IllegalStateException( "Already an RMI object bound for name '" + this.serviceName + "': " + ex.toString()); } catch (RemoteException ex) { unexportObjectSilently(); throw ex; } } ……}
7.RemoteInvocationBasedExporter处理RMI远程调用请求:
RmiServiceExporter的父类RmiBasedExporter的父类RemoteInvocationBasedExporter负责对RMI远程调用请求进行处理,并将处理的结果封装返回,其源码如下:
[java] view plaincopyprint public abstract class RemoteInvocationBasedExporter extends RemoteExporter { //RMI远程调用处理器,RMI远程调用请求是由DefaultRemoteInvocationExecutor处理 private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor(); //设置RMI远程调用处理器 public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) { this.remoteInvocationExecutor = remoteInvocationExecutor; } //获取RMI远程调用处理器 public RemoteInvocationExecutor getRemoteInvocationExecutor() { return this.remoteInvocationExecutor; } //对RMI远程调用请求进行处理的地方 protected Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (logger.isTraceEnabled()) { logger.trace("Executing " + invocation); } try { //调用DefaultRemoteInvocationExecutor对RMI远程调用请求进行处理 return getRemoteInvocationExecutor().invoke(invocation, targetObject); } catch (NoSuchMethodException ex) { if (logger.isDebugEnabled()) { logger.warn("Could not find target method for " + invocation, ex); } throw ex; } catch (IllegalAccessException ex) { if (logger.isDebugEnabled()) { logger.warn("Could not access target method for " + invocation, ex); } throw ex; } catch (InvocationTargetException ex) { if (logger.isDebugEnabled()) { logger.debug("Target method failed for " + invocation, ex.getTargetException()); } throw ex; } } //获取RMI远程调用请求的处理结果 protected RemoteInvocationResult inv