ITeye博客

hessian——源码分析(二)------ HessianProxy《转》

goon · 2013-02-26 20:35 · Hessian · 编程语言 · Markdown

Hessian在客户端一块采用Proxy模式,当客户端调用远程接口时,HessianProxy会代理这个动作,在invoke方法中,把客户端请求的方法和参数序列化为预订格式的输出流,主要流程如下图所示:

 

下面我将详细解析一下invoke源码:

 

[java] view plaincopy

  1. public Object invoke(Object proxy, Method method, Object []args)
  2. throws Throwable
  3. {
  4. String mangleName;
  5. synchronized (_mangleMap) {
  6. mangleName = _mangleMap.get(method);
  7. }
  8. if (mangleName == null) {
  9. String methodName = method.getName();
  10. Class []params = method.getParameterTypes();
  11. // equals and hashCode are special cased
  12. if (methodName.equals("equals")
  13. && params.length == 1 && params[0].equals(Object.class)) {
  14. Object value = args[0];
  15. if (value == null || ! Proxy.isProxyClass(value.getClass()))
  16. return new Boolean(false);
  17. HessianProxy handler = (HessianProxy) Proxy.getInvocationHandler(value);
  18. return new Boolean(_url.equals(handler.getURL()));
  19. }
  20. else if (methodName.equals("hashCode") && params.length == 0)
  21. return new Integer(_url.hashCode());
  22. else if (methodName.equals("getHessianType"))
  23. return proxy.getClass().getInterfaces()[0].getName();
  24. else if (methodName.equals("getHessianURL"))
  25. return _url.toString();
  26. else if (methodName.equals("toString") && params.length == 0)
  27. return "HessianProxy[" + _url + "]";
  28. if (! _factory.isOverloadEnabled())
  29. mangleName = method.getName();
  30. else
  31. mangleName = mangleName(method);
  32. synchronized (_mangleMap) {
  33. _mangleMap.put(method, mangleName);
  34. }
  35. }
  36. ......
  37. }

 

这是invoke的开头,主要干的事情是把methodName缓存起来和过滤一些特殊调用,java反射是个比较耗性能的操作,把methodName缓存起来可以避免每次调用都要从method里得到methodName。另外,对equals、hashCode、getHessianType、getHessianURL等特殊方法的远程调用,HessianProxy不会走远程调用,而是直接返回。

接着往下看:

 

[java] view plaincopy

  1. public Object invoke(Object proxy, Method method, Object []args)
  2. throws Throwable
  3. {
  4. ......
  5. InputStream is = null;
  6. URLConnection conn = null;
  7. HttpURLConnection httpConn = null;
  8. try {
  9. conn = sendRequest(mangleName, args);
  10. ......
  11. } catch (HessianProtocolException e) {
  12. throw new HessianRuntimeException(e);
  13. } finally {
  14. ......
  15. }
  16. }

 

这段主要是把方法名和参数序列化为网络输出流,并做网络请求,具体逻辑包装在sendRequest方法中:

 

[java] view plaincopy

  1. protected URLConnection sendRequest(String methodName, Object []args)
  2. throws IOException
  3. {
  4. URLConnection conn = null;
  5. conn = _factory.openConnection(_url);
  6. boolean isValid = false;
  7. try {
  8. // Used chunked mode when available, i.e. JDK 1.5.
  9. if (_factory.isChunkedPost() && conn instanceof HttpURLConnection) {
  10. try {
  11. HttpURLConnection httpConn = (HttpURLConnection) conn;
  12. httpConn.setChunkedStreamingMode(8 * 1024);
  13. } catch (Throwable e) {
  14. }
  15. }
  16. addRequestHeaders(conn);
  17. OutputStream os = null;
  18. try {
  19. os = conn.getOutputStream();
  20. } catch (Exception e) {
  21. throw new HessianRuntimeException(e);
  22. }
  23. if (log.isLoggable(Level.FINEST)) {
  24. PrintWriter dbg = new PrintWriter(new LogWriter(log));
  25. os = new HessianDebugOutputStream(os, dbg);
  26. }
  27. AbstractHessianOutput out = _factory.getHessianOutput(os);
  28. out.call(methodName, args);
  29. out.flush();
  30. isValid = true;
  31. return conn;
  32. } finally {
  33. if (! isValid && conn instanceof HttpURLConnection)
  34. ((HttpURLConnection) conn).disconnect();
  35. }
  36. }

 

sendRequest的主要流程是先初始化网络连接,然后用AbstractHessianOutput包装网络输出流,调用AbstractHessianOutput.call(methodName, args)完成网络输出,这个方法的细节会在hessian io的源码解析中详细分析。

下面接着看invoke方法:

 

[java] view plaincopy

  1. public Object invoke(Object proxy, Method method, Object []args)
  2. throws Throwable
  3. {
  4. ......
  5. is = conn.getInputStream();
  6. AbstractHessianInput in = _factory.getHessianInput(is);
  7. in.startReply();
  8. Object value = in.readObject(method.getReturnType());
  9. if (value instanceof InputStream) {
  10. value = new ResultInputStream(httpConn, is, in, (InputStream) value);
  11. is = null;
  12. httpConn = null;
  13. }
  14. else
  15. in.completeReply();
  16. ......
  17. }

 

这一段主要是把输入流中取得返回值,具体是用AbstractHessianInput包装网络输入流,然后调用AbstractHessianInput.readObject从网络流中反序列化到实际返回值。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。