首页 > 代码库 > hessian(转)
hessian(转)
Hessian——轻量级远程调用方案
Hessian是caucho公司开发的一种基于二进制RPC协议(Remote Procedure Call protocol)的轻量级远程调用框架。具有多种语言的实现,但用的最多的当然是Java实现 :)
Creating a Hessian service using Java has four steps:
1、Create an Java interface as the public API
2、Create a client using HessianProxyFactory
3、Create the Service implementation class
4、Configure the service in your servlet engine.
下面,我们来实现1个例子:
首先下载hessian-4.0.37.jar,服务端和客户端都要用的。
然后编写程序。
server端:
package com.lin.hessian.service; public interface Basic { public String sayHello(); }
package com.lin.hessian.service; public class BasicService implements Basic{ private String message = "balalala!"; @Override public String sayHello() { return message; } }
package com.lin.hessian.service; import com.caucho.hessian.server.HessianServlet; public class LinyService extends HessianServlet implements Basic{ private String message = "wowwowowowowow"; @Override public String sayHello() { return message; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>HessianWeb</display-name> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-class</param-name> <param-value>com.lin.hessian.service.BasicService</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>com.lin.hessian.service.Basic</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>liny</servlet-name> <servlet-class>com.lin.hessian.service.LinyService</servlet-class> </servlet> <servlet-mapping> <servlet-name>liny</servlet-name> <url-pattern>/liny</url-pattern> </servlet-mapping> </web-app>
在服务端其实定义了两个service: BasicService和LinyService,不同之处在于是否继承HessianServlet,BasicService是一个pojo,而LinyService是一个servlet。
在web.xml里,BasicService是通过home-class和home-api两个参数传递给HessianServlet,然后将HessianServlet配置到web.xml的<servlet>里来实现服务配置到容器的。
而LinyService是直接将自己(它自己就是个Servlet)配置到web.xml的<servlet>来实现配置到容器。如果我们在一个应用中要实现多个hessian服务,应该采用这种方式。
下面来看看客户端:
package com.liny.hessian.service; public interface Basic { public String sayHello(); }
package com.lin.hessian.client; import java.net.MalformedURLException; import com.caucho.hessian.client.HessianProxyFactory; import com.liny.hessian.service.Basic; public class HessianClient { public static void main(String[] args) throws MalformedURLException{
//String url = "http://localhost:8080/HessianWeb/hello"; String url = "http://localhost:8080/HessianWeb/liny"; HessianProxyFactory factory = new HessianProxyFactory(); Basic basic = (Basic)factory.create(Basic.class, url); System.out.println(basic.sayHello()); } }
客户端要定义一个同服务端一模一样的接口Basic,然后通过HessianProxyFactory获得代理,并调用远程服务的方法。
注意:这里笔者故意将客户端与服务端的Basic接口的包路径定义成不一样(一个是com.liny.hessian.service,一个是com.lin.hessian.service),经过验证这样是可以的,但是推荐最好两者一模一样。
日子过得艰苦,但更要坚韧不拔,每天都能进步一点的话,许多天之后必能精进。
hessian(转)