Hessian服务端核心对象HessianSkeleton 主要做两件事
1.把客户端请求的流反序列化 得到 对应的方法名称 参数
2. 服务类(service)对应方法执行完成 把结果序列化到输出流
hessian服务端暴露服务是通过HessianServlet
- public class HessianServlet extends GenericServlet {
- }
public class HessianServlet extends GenericServlet { }
HessianServlet 的init方法主要初始化服务类(service) 和HessianSkeleton 每一个服务类对应一个HessianSkeleton 在HessianSkeleton中持有业务类的引用
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- //web.xml中home-class,service-class配置服务的实现类
- if (_homeImpl != null) {
- } else if (getInitParameter("home-class") != null) {
- String className = getInitParameter("home-class");
- Class homeClass = loadClass(className);
- _homeImpl = homeClass.newInstance();
- init(_homeImpl);
- } else if (getInitParameter("service-class") != null) {
- String className = getInitParameter("service-class");
- Class homeClass = loadClass(className);
- _homeImpl = homeClass.newInstance();
- init(_homeImpl);
- } else {
- if (getClass().equals(HessianServlet.class))
- throw new ServletException(
- "server must extend HessianServlet");
- _homeImpl = this;
- }
- //web.xml配置的 服务类实现接口
- if (_homeAPI != null) {
- } else if (getInitParameter("home-api") != null) {
- String className = getInitParameter("home-api");
- _homeAPI = loadClass(className);
- } else if (getInitParameter("api-class") != null) {
- String className = getInitParameter("api-class");
- _homeAPI = loadClass(className);
- } else if (_homeImpl != null) {
- //如果没有配置服务实现类的接口 递归 找到服务类最顶层的接口
- _homeAPI = findRemoteAPI(_homeImpl.getClass());
- if (_homeAPI == null)
- _homeAPI = _homeImpl.getClass();
- }
- //TODO
- _homeSkeleton = new HessianSkeleton(_homeImpl, _homeAPI);
- }
public void init(ServletConfig config) throws ServletException { super.init(config); //web.xml中home-class,service-class配置服务的实现类 if (_homeImpl != null) { } else if (getInitParameter("home-class") != null) { String className = getInitParameter("home-class"); Class homeClass = loadClass(className); _homeImpl = homeClass.newInstance(); init(_homeImpl); } else if (getInitParameter("service-class") != null) { String className = getInitParameter("service-class"); Class homeClass = loadClass(className); _homeImpl = homeClass.newInstance(); init(_homeImpl); } else { if (getClass().equals(HessianServlet.class)) throw new ServletException( "server must extend HessianServlet"); _homeImpl = this; } //web.xml配置的 服务类实现接口 if (_homeAPI != null) { } else if (getInitParameter("home-api") != null) { String className = getInitParameter("home-api"); _homeAPI = loadClass(className); } else if (getInitParameter("api-class") != null) { String className = getInitParameter("api-class"); _homeAPI = loadClass(className); } else if (_homeImpl != null) { //如果没有配置服务实现类的接口 递归 找到服务类最顶层的接口 _homeAPI = findRemoteAPI(_homeImpl.getClass()); if (_homeAPI == null) _homeAPI = _homeImpl.getClass(); } //TODO _homeSkeleton = new HessianSkeleton(_homeImpl, _homeAPI); }
service方法
- public void service(ServletRequest request, ServletResponse response)
- throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) request;
- HttpServletResponse res = (HttpServletResponse) response;
- //必须是post方式
- if (!req.getMethod().equals("POST")) {
- res.setStatus(500, "Hessian Requires POST");
- PrintWriter out = res.getWriter();
- res.setContentType("text/html");
- out.println("<h1>Hessian Requires POST</h1>");
- return;
- }
- String serviceId = req.getPathInfo();
- String objectId = req.getParameter("id");
- if (objectId == null)
- objectId = req.getParameter("ejbid");
- //保存到ThreadLocal中
- ServiceContext.begin(req, serviceId, objectId);
- try {
- InputStream is = request.getInputStream();
- OutputStream os = response.getOutputStream();
- response.setContentType("application/x-hessian");
- SerializerFactory serializerFactory = getSerializerFactory();
- //交给HessianSkeleton invoke方法处理
- invoke(is, os, objectId, serializerFactory);
- } finally {
- ServiceContext.end();
- }
- }
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; //必须是post方式 if (!req.getMethod().equals("POST")) { res.setStatus(500, "Hessian Requires POST"); PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<h1>Hessian Requires POST</h1>"); return; } String serviceId = req.getPathInfo(); String objectId = req.getParameter("id"); if (objectId == null) objectId = req.getParameter("ejbid"); //保存到ThreadLocal中 ServiceContext.begin(req, serviceId, objectId); try { InputStream is = request.getInputStream(); OutputStream os = response.getOutputStream(); response.setContentType("application/x-hessian"); SerializerFactory serializerFactory = getSerializerFactory(); //交给HessianSkeleton invoke方法处理 invoke(is, os, objectId, serializerFactory); } finally { ServiceContext.end(); } }
HessianSkeleton invoke方法核心代码
- public void invoke(Object service,
- AbstractHessianInput in,
- AbstractHessianOutput out)
- throws Exception
- {
- //..略
- //反序列化 获得方法名称
- String methodName = in.readMethod();
- int argLength = in.readMethodArgLength();
- Method method;
- //_methodMap中获得对应的方法
- method = getMethod(methodName + "__" + argLength);
- // ..略
- Class<?> []args = method.getParameterTypes();
- //获得方法参数
- Object []values = new Object[args.length];
- for (int i = 0; i < args.length; i++) {
- // XXX: needs Marshal object
- values[i] = in.readObject(args[i]);
- }
- Object result = null;
- try {
- result = method.invoke(service, values);
- } catch (Exception e) {
- //...略
- return;
- }
- in.completeCall();
- // AbstractHessianOutput 序列化方法返回值
- out.writeReply(result);
- out.close();
- }
public void invoke(Object service, AbstractHessianInput in, AbstractHessianOutput out) throws Exception { //..略 //反序列化 获得方法名称 String methodName = in.readMethod(); int argLength = in.readMethodArgLength(); Method method; //_methodMap中获得对应的方法 method = getMethod(methodName + "__" + argLength); // ..略 Class<?> []args = method.getParameterTypes(); //获得方法参数 Object []values = new Object[args.length]; for (int i = 0; i < args.length; i++) { // XXX: needs Marshal object values[i] = in.readObject(args[i]); } Object result = null; try { result = method.invoke(service, values); } catch (Exception e) { //...略 return; } in.completeCall(); // AbstractHessianOutput 序列化方法返回值 out.writeReply(result); out.close(); }
相关推荐
com.alibaba:hessian-lite:jar:3.2.1-fixed-2 hessian-lite hessian-lite-3.2.1-fixed-2.jar
赠送jar包:hessian-3.3.6.jar 赠送原API文档:hessian-3.3.6-javadoc.jar 赠送源代码:hessian-3.3.6-sources.jar 包含翻译后的API文档:hessian-3.3.6-javadoc-API文档-中文(简体)-英语-对照版.zip 对应Maven...
赠送jar包:hessian-3.3.6.jar; 赠送原API文档:hessian-3.3.6-javadoc.jar; 赠送源代码:hessian-3.3.6-sources.jar; 赠送Maven依赖信息文件:hessian-3.3.6.pom; 包含翻译后的API文档:hessian-3.3.6-javadoc-...
Hessian 框架源码jar包 hessian-4.0.7-src.jar
hessian是一个轻量级的Java Remoting方案
hessian源码
标题 "hessian-flash-3.1.2.zip" 指的是一个包含 Hessian 协议相关组件的软件包,版本为 3.1.2,且与 Flash 技术相结合。Hessian 是一种二进制的远程过程调用(RPC)协议,由 Caucho Technology 开发,它能高效地...
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
5. **源码附赠**:刘骥的讲座可能包含了Hessian服务的示例源码,这对于学习和理解Hessian的工作原理及其实际应用非常有帮助。通过分析和实践这些源码,开发者可以深入理解如何在自己的项目中集成和使用Hessian。 6....
java运行依赖jar包
赠送jar包:hessian-4.0.63.jar; 赠送原API文档:hessian-4.0.63-javadoc.jar; 赠送源代码:hessian-4.0.63-sources.jar; 赠送Maven依赖信息文件:hessian-4.0.63.pom; 包含翻译后的API文档:hessian-4.0.63-...
hessian.jarhessian.jarhessian.jarhessian.jarhessian.jarhessian.jarhessian.jar
Hessian是一种二进制Web服务协议,它提供了一种高效的RPC(远程过程调用)机制,使得服务器和客户端之间能够进行快速、简单的通信。在Java环境中,Hessian库被广泛用于构建分布式应用,因为它可以将Java对象序列化为...
3. `hessian-lite.jar`:轻量级版本的Hessian库,可能只包含了基础功能,适用于对体积有严格要求的环境。 4. `hessian-serial.jar`:可能包含了额外的序列化支持,比如对于某些特定类型或第三方库的支持。 使用这些...
首先,`hessian-4.0.7.jar`文件是预编译的Hessian库,可以直接在Java项目中使用。它提供了各种类和接口,如`com.caucho.hessian.io.Hessian2Input`和`com.caucho.hessian.io.Hessian2Output`,用于序列化和反序列化...
hessian与flex交互,所以用到的swc包
在实际开发过程中,可能会遇到由于阿里的网络限制导致无法直接获取 hessian-lite 这个依赖的问题,此时需要用户手动下载 hessian-lite 源码,通过 Maven 安装到本地仓库以解决依赖。 【描述】中提到的 "dubbo源码...
1. **获取源码**:首先,从官方渠道或指定仓库下载hessian-lite的源码包,例如这里给出的是名为“hessian-lite-master”的压缩包。 2. **构建并安装**:解压源码包,使用Maven工具执行`mvn install`命令,将Hessian...