Spring框架学习[Spring使用Hessian实现远程调用](二)

2014-11-24 03:00:31 · 作者: · 浏览: 1
izerFactory(SerializerFactory serializerFactory) { this.proxyFactory.setSerializerFactory(serializerFactory); } //设置Hessian是否发送java集合类型对象 public void setSendCollectionType(boolean sendCollectionType) { this.proxyFactory.getSerializerFactory().setSendCollectionType(sendCollectionType); } //设置远程调用时是否重载方法 public void setOverloadEnabled(boolean overloadEnabled) { this.proxyFactory.setOverloadEnabled(overloadEnabled); } //设置远程调用用户名 public void setUsername(String username) { this.proxyFactory.setUser(username); } //设置远程调用密码 public void setPassword(String password) { this.proxyFactory.setPassword(password); } //设置是否使用Hessian的Debug调试模式 public void setDebug(boolean debug) { this.proxyFactory.setDebug(debug); } //设置是否使用chunked端口发送Hessian请求 public void setChunkedPost(boolean chunkedPost) { this.proxyFactory.setChunkedPost(chunkedPost); } //设置Hessian等待响应的超时时长 public void setReadTimeout(long timeout) { this.proxyFactory.setReadTimeout(timeout); } //设置是否使用Hessain版本2协议解析请求和响应 public void setHessian2(boolean hessian2) { this.proxyFactory.setHessian2Request(hessian2); this.proxyFactory.setHessian2Reply(hessian2); } //设置是否使用Hessian版本2协议解析请求 public void setHessian2Request(boolean hessian2) { this.proxyFactory.setHessian2Request(hessian2); } //设置是否使用Hessian版本2协议解析响应 public void setHessian2Reply(boolean hessian2) { this.proxyFactory.setHessian2Reply(hessian2); } //子类HessianProxyFactoryBean的回调方法调用此回调方法 public void afterPropertiesSet() { //调用其父类UrlBasedRemoteAccessor的回调方法获取客户端配置的请求url super.afterPropertiesSet(); //初始化Hessian代理 prepare(); } //初始化Hessian代理 public void prepare(
) throws RemoteLookupFailureException { try { //创建Hessian代理 this.hessianProxy = createHessianProxy(this.proxyFactory); } catch (MalformedURLException ex) { throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex); } } //创建Hessian代理 protected Object createHessianProxy(HessianProxyFactory proxyFactory) throws MalformedURLException { Assert.notNull(getServiceInterface(), "'serviceInterface' is required"); //使用Hessian代理工厂创建Hessian代理 return proxyFactory.create(getServiceInterface(), getServiceUrl()); } //拦截器客户端请求的方法 public Object invoke(MethodInvocation invocation) throws Throwable { if (this.hessianProxy == null) { throw new IllegalStateException("HessianClientInterceptor is not properly initialized - " + "invoke 'prepare' before attempting any operations"); } //获取当前环境中线程类加载器 ClassLoader originalClassLoader = overrideThreadContextClassLoader(); try { //调用Hessian代理的方法,是Hessian远程调用的入口方法,使用JDK反射机制 return invocation.getMethod().invoke(this.hessianProxy, invocation.getArguments()); } //处理Hessian远程调用中的异常 catch (InvocationTargetException ex) { Throwable targetEx = ex.getTargetException(); if (targetEx instanceof InvocationTargetException) { targetEx = ((InvocationTargetException) targetEx).getTargetException(); } if (targetEx instanceof HessianConnectionException) { throw convertHessianAccessException(targetEx); } else if (targetEx instanceof HessianException || targetEx instanceof HessianRuntimeException) { Throwable cause = targetEx.getCause(); throw convertHessianAccessException(cause != null cause : targetEx); } else if (targetEx instanceof UndeclaredThrowableException) { UndeclaredThrowableException utex =