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

2014-11-24 03:00:37 · 作者: · 浏览: 10
xy()); //将调用结果写到HTTP响应中 writeRemoteInvocationResult(request, response, result); } catch (ClassNotFoundException ex) { throw new NestedServletException("Class not found during deserialization", ex); } } //从HTTP请求中读取RemoteInvocation远程调用对象入口方法 protected RemoteInvocation readRemoteInvocation(HttpServletRequest request) throws IOException, ClassNotFoundException { //将从HTTP请求中读取远程调用对象 return readRemoteInvocation(request, request.getInputStream()); } //从HTTP请求中读取远程调用对象 protected RemoteInvocation readRemoteInvocation(HttpServletRequest request, InputStream is) throws IOException, ClassNotFoundException { //根据HTTP请求输入流创建对象输入流 ObjectInputStream ois = createObjectInputStream(decorateInputStream(request, is)); try { //从对象输入流中读取远程调用对象 return doReadRemoteInvocation(ois); } finally { ois.close(); } } //获取HTTP请求输入流 protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException { return is; } //将远程调用执行结果写到HTTP响应中 protected void writeRemoteInvocationResult( HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result) throws IOException { //设置HTTP响应的内容类型为:application/x-java-serialized-object response.setContentType(getContentType()); //将远程调用结果写到HTTP响应中 writeRemoteInvocationResult(request, response, result, response.getOutputStream()); } //将远程调用执行结果写入HTTP响应中 protected void writeRemoteInvocationResult( HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os) throws IOException { //获取HTTP响应对象输出流 ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os)); try { //将远程调用执行结果写到HTTP响应对象输出流中 doWriteRemoteInvocationResult(result, oos); } finally { oos.close(); } } //获取HTTP响应对象输入流 protected OutputStream decorateOutputStream( HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException { return os; } }

通过对HttpInvokerServiceExporter的源码分析,我们可以看出,真正执行远程对象调用的是RemoteInvocationResultresult = invokeAndCreateResult(invocation, getProxy());它调用了RemoteInvocationBasedExporter的invokeAndCreateResult方法调用远程目标对象方法,并创建远程调用执行结果,下面我们继续分析执行服务器端远程调用目标对象方法的实现。

9.RemoteInvocationBasedExporter调用服务器目标对象:

RemoteInvocationBasedExporter的invokeAndCreateResult方法调用服务器目标对象方法,RemoteInvocationBasedExporter源码如下:

[java] view plaincopyprint
  1. public abstract class RemoteInvocationBasedExporter extends RemoteExporter { //远程调用执行器
  2. private RemoteInvocationExecutor remoteInvocationExecutor = new DefaultRemoteInvocationExecutor(); public RemoteInvocationExecutor getRemoteInvocationExecutor() {
  3. return this.remoteInvocationExecutor; }
  4. protected RemoteInvocationResult invokeAndCreateResult(RemoteInvocation invocation, Object targetObject) { try {
  5. //调用服务器端目标对象的方法 Object value = invoke(invocation, targetObject);
  6. //根据执行结果创建RemoteInvocationResult return new RemoteInvocationResult(value);
  7. } catch (Throwable ex) {
  8. return new RemoteInvocationResult(ex); }
  9. } //调用目标对象的方法
  10. protected Object invoke(RemoteInvocation invocation, Obje