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

2014-11-24 03:00:32 · 作者: · 浏览: 10
置RMI服务端socket工厂 public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) { this.serverSocketFactory = serverSocketFactory; } //设置RMI注册器 public void setRegistry(Registry registry) { this.registry = registry; } //设置RMI注册主机 public void setRegistryHost(String registryHost) { this.registryHost = registryHost; } //设置RMI注册端口 public void setRegistryPort(int registryPort) { this.registryPort = registryPort; } //设置用于注册的RMI客户端socket工厂 public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) { this.registryClientSocketFactory = registryClientSocketFactory; } //设置用于注册的RMI服务端socket工厂 public void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory) { this.registryServerSocketFactory = registryServerSocketFactory; } //设置是否总是创建注册,而不是试图查找指定端口上已有的注册 public void setAlwaysCreateRegistry(boolean alwaysCreateRegistry) { this.alwaysCreateRegistry = alwaysCreateRegistry; } //设置是否提供已绑定的RMI注册 public void setReplaceExistingBinding(boolean replaceExistingBinding) { this.replaceExistingBinding = replaceExistingBinding; } //IoC容器依赖注入完成之后的回调方法 public void afterPropertiesSet() throws RemoteException { prepare(); } //初始化RMI服务导出器 public void prepare() throws RemoteException { //调用其父类RmiBasedExporter的方法,检查服务引用是否被设置 checkService(); //如果服务导出名称为null if (this.serviceName == null) { throw new IllegalArgumentException("Property 'serviceName' is required"); } //检查socket工厂 if (this.clientSocketFactory instanceof RMIServerSocketFactory) { this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory;
} if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || (this.clientSocketFactory == null && this.serverSocketFactory != null)) { throw new IllegalArgumentException( "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); } //检查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 '