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

2014-11-24 03:00:37 · 作者: · 浏览: 3
用远程调用代理的方法时,将会触发HttpInvokerClientInterceptor拦截器的invoke方法对当前的请求进行封装处理,将客户端的java对象序列化传输到服务器端,在远程服务器端执行完请求之后,又将处理结果java对象序列化返回给客户端。其源码如下:

[java] view plaincopyprint
  1. public class HttpInvokerClientInterceptor extends RemoteInvocationBasedAccessor implements MethodInterceptor, HttpInvokerClientConfiguration {
  2. private String codebaseUrl; //HTTP调用请求执行器
  3. private HttpInvokerRequestExecutor httpInvokerRequestExecutor; public void setCodebaseUrl(String codebaseUrl) {
  4. this.codebaseUrl = codebaseUrl; }
  5. public String getCodebaseUrl() { return this.codebaseUrl;
  6. } public void setHttpInvokerRequestExecutor(HttpInvokerRequestExecutor httpInvokerRequestExecutor) {
  7. this.httpInvokerRequestExecutor = httpInvokerRequestExecutor; }
  8. //获取HTTP调用请求执行器,如果HTTP调用请求执行器没有设置,则使用 //SimpleHttpInvokerRequestExecutor作为HTTP调用请求执行器
  9. public HttpInvokerRequestExecutor getHttpInvokerRequestExecutor() { if (this.httpInvokerRequestExecutor == null) {
  10. SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor(); executor.setBeanClassLoader(getBeanClassLoader());
  11. this.httpInvokerRequestExecutor = executor; }
  12. return this.httpInvokerRequestExecutor; }
  13. //IoC容器初始化完成回调方法 public void afterPropertiesSet() {
  14. //调用父类的初始化回调方法 super.afterPropertiesSet();
  15. //获取HTTP调用请求执行器 getHttpInvokerRequestExecutor();
  16. } //拦截器代理对象方法调用入口,拦截器将客户端对远程调用代理的调用封装为
  17. //MethodInvocation对象。 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  18. if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";
  19. } //创建远程调用对象,封装了远程调用
  20. RemoteInvocation invocation = createRemoteInvocation(methodInvocation); //远程调用结果
  21. RemoteInvocationResult result = null; try {
  22. //远程调用入口 result = executeRequest(invocation, methodInvocation);
  23. } catch (Throwable ex) {
  24. throw convertHttpInvokerAccessException(ex); }
  25. try { //返回远程调用结果
  26. return recreateRemoteInvocationResult(result); }
  27. catch (Throwable ex) { if (result.hasInvocationTargetException()) {
  28. throw ex; }
  29. else { throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +
  30. "] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex); }
  31. } }
  32. //执行远程调用入口 protected RemoteInvocationResult executeRequest(
  33. RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception { return executeRequest(invocation);
  34. } //通过HTTP调用请求执行器执行远程调用
  35. protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws Exception { return getHttpInvokerRequestExecutor().executeRequest(this, invocation);
  36. } //将远程调用异常转换成Spring异常
  37. protected RemoteAccessException convertHttpInvokerAccessException(Throwable ex) { if (ex instanceof ConnectException) {
  38. throw new RemoteConnectFailureException( "Could not connect to HTTP invoker remote service at [" + getServiceUrl() + "]", ex);
  39. } else if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError ||
  40. ex instanceof InvalidClassException) { throw new RemoteAccessException(
  41. "Could not deserialize result from HTTP invoker remote service [" + getServiceUrl() + "]", ex); }
  42. else { throw new RemoteAccessException(
  43. "Could not access HTTP invoker remote service at [" + getServiceUrl() + "]", ex); }
  44. } }