Hessian源码浅析-HessianProxy
来源:互联网 发布:淘宝详情上传 编辑:程序博客网 时间:2024/05/22 08:14
转载自:http://blog.csdn.net/java2000_wl/article/details/7560393
Hessian客户端主要是通过proxy代理来实现 当客户端调用远程接口方法时 会被HessianProxy 代理 HessianProxy invoke方法主要做以下工作
1.把调用的 方法名称 参数 序列化
2.通过HttpURLConnection向服务端发送调用请求
3.服务端返回的结果 反序列化
Proxy是由HessianProxyFactory创建
HessianProxyFactory的create方法
- public Object create(Class<?> api, URL url, ClassLoader loader) {
- if (api == null)
- throw new NullPointerException(
- "api must not be null for HessianProxyFactory.create()");
- InvocationHandler handler = null;
- //api 远程接口
- handler = new HessianProxy(url, this, api);
- return Proxy.newProxyInstance(loader, new Class[] { api, HessianRemoteObject.class }, handler);
- }
当调用远程接口的方法时 会激活HessianProxy代理的invoke方法 invoke方法主要向服务端发送请求 反序列化服务端返回的结果
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- String mangleName;
- // 从缓存中获取methodName
- synchronized (_mangleMap) {
- mangleName = _mangleMap.get(method);
- }
- // 如果缓存中没有
- if (mangleName == null) {
- String methodName = method.getName();
- Class<?>[] params = method.getParameterTypes();
- //如果是 equals, hashcode ,getHessianType,getHessianURL
- //直接调用url的对应方法返回结果 不会向服务端发送请求
- if (methodName.equals("equals") && params.length == 1
- && params[0].equals(Object.class)) {
- Object value = args[0];
- if (value == null || !Proxy.isProxyClass(value.getClass()))
- return Boolean.FALSE;
- Object proxyHandler = Proxy.getInvocationHandler(value);
- if (!(proxyHandler instanceof HessianProxy))
- return Boolean.FALSE;
- HessianProxy handler = (HessianProxy) proxyHandler;
- return new Boolean(_url.equals(handler.getURL()));
- } else if (methodName.equals("hashCode") && params.length == 0)
- return new Integer(_url.hashCode());
- else if (methodName.equals("getHessianType"))
- return proxy.getClass().getInterfaces()[0].getName();
- else if (methodName.equals("getHessianURL"))
- return _url.toString();
- else if (methodName.equals("toString") && params.length == 0)
- return "HessianProxy[" + _url + "]";
- //重载方法支持 默认false
- if (!_factory.isOverloadEnabled())
- mangleName = method.getName();
- else
- //重载方法处理 最终已 methodName_参数类型_参数类型..形式返回
- mangleName = mangleName(method);
- //缓存起来
- synchronized (_mangleMap) {
- _mangleMap.put(method, mangleName);
- }
- }
- InputStream is = null;
- HessianConnection conn = null;
- try {
- //序列化 方法名称 参数 向服务端发出请求
- //mangleName 方法定义 可能是methodName_参数类型_参数类型..
- //args 参数
- conn = sendRequest(mangleName, args);
- //获得HttpURLConnection 的输入流
- is = conn.getInputStream();
- AbstractHessianInput in;
- int major = is.read();
- int minor = is.read();
- in = _factory.getHessianInput(is);
- in.startReplyBody();
- //反序列化服务端返回结果
- Object value = in.readObject(method.getReturnType());
- if (value instanceof InputStream) {
- value = new ResultInputStream(conn, is, in, (InputStream) value);
- is = null;
- conn = null;
- } else
- in.completeReply();
- //返回最终结果
- return value;
- } catch (HessianProtocolException e) {
- throw new HessianRuntimeException(e);
- } finally {
- //..略
- }
- }
sendRequest 方法主要序列化请求信息(方法,参数) 然后向服务端发送请求 HessianConnection包装了HttpURLConnection
- protected HessianConnection sendRequest(String methodName, Object[] args)
- throws IOException {
- //包装了java HttpURLConnection
- HessianConnection conn = null;
- conn = _factory.getConnectionFactory().open(_url);
- boolean isValid = false;
- try {
- addRequestHeaders(conn);
- OutputStream os = null;
- try {
- os = conn.getOutputStream();
- } catch (Exception e) {
- throw new HessianRuntimeException(e);
- }
- AbstractHessianOutput out = _factory.getHessianOutput(os);
- //序列化
- out.call(methodName, args);
- out.flush();
- //发出网络请求
- conn.sendRequest();
- isValid = true;
- return conn;
- } finally {
- if (!isValid && conn != null)
- conn.destroy();
- }
- }
0 0
- Hessian源码浅析-HessianProxy
- Hessian源码浅析-HessianProxy
- Hessian源码浅析-HessianProxy
- Hessian源码分析--HessianProxy
- Hessian源码分析(二)------ HessianProxy
- Hessian源码分析(二)------ HessianProxy
- Hessian源码浅析-HessianSkeleton
- hessian java发布远程服务中出现的 “com.caucho.hessian.client.HessianConnectionException HessianProxy ”的问题原因总结:
- hessian源码,com.caucho.hessian
- hessian源码分析
- hessian源码分析-服务器端
- Hessian源码学习(1)
- Hessian源码分析--HessianProxyFactory
- Hessian源码分析--HessianServlet
- Hessian源码分析--HessianSkeleton
- Hessian源码分析(java)
- Hessian 源码简单分析
- Hessian源码学习(2)
- ORA-04043 object DROP_AW_ELIST_ALL does not exist
- python多进程与多线程之间的联系
- linux常用命令举例
- input file 上传文件后显示出来
- HDU3658 How many words 矩阵快速幂
- Hessian源码浅析-HessianProxy
- 关于js中正则的使用
- media的两张图,以前画的
- 【git】 无法commit问题
- Oracle:ORA-01810:格式代码出现两次 日期格式、时间格式、to_date错误
- PHP 获取 今日 本月 上月
- apache 中的AB测试
- 在Windows端搭建Redmine项目管理软件
- MPChart的饼状图使用