1.简介
Hessian是一个轻量级的remoting on http工具,通过简单的方法提供了RMI的功能。 与WebService相比较而言,Hessian更简单、快捷。
Hessian采用的是二进制的RPC协议,正是因为它采用的是二进制协议,所以它很适合于发送二进制数据。
2.实例讲解
服务端:
a.下载hessian.jar,链接地址:http://hessian.caucho.com/#Java,选择所需要的版本。
b.在开发服务端的时候是web工程。
c.代码实现如下:
public interface MyHessianInterface { public void say(String str); public String hello(); public PeopleInfo getUser(); }
public class MyHessian implements MyHessianInterface { private String str = "Hello Hessian" ; @Override public void say(String str) { this.str = str ; System.out.println("setting say success: " + str); } @Override public String hello() { return str; } @Override public PeopleInfo getUser() { return new PeopleInfo("Apple","apple@1234"); } }
public class PeopleInfo implements Serializable { private static final long serialVersionUID = -1938252045479387918L; private String people_name = "Hessian" ; private String people_password = "hessian@1234"; public PeopleInfo(String people_name,String people_password){ this.people_name = people_name ; this.people_password = people_password; } public String getPeopleName(){ return people_name ; } public String getPeoplePassword(){ return people_password ; } }
d.web.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>hessian</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>service-class</param-name> <param-value>com.zh.hessian.MyHessian</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian</url-pattern> </servlet-mapping> </web-app>
客户端:
a.必须加载hessian.jar
b.在开发客户端的时候是Java工程
c.代码实现:
public interface HessianClientInteface { public void say(String str); public String hello(); public PeopleInfo getUser(); }
public class PeopleInfo implements Serializable { private static final long serialVersionUID = 8268991521882061845L; private String people_name = "hello hessian" ; private String people_password = "hello hessian@1234" ; public PeopleInfo(String people_name,String people_password){ this.people_name = people_name ; this.people_password = people_password ; } public String getPeopleName(){ return people_name ; } public String getPeoplePassword(){ return people_password ; } }
public class MyHessianClient { public static void main(String[] args) throws Exception{ String url = "http://127.0.0.1:8080/JavaDemo/hessian" ; HessianProxyFactory factory = new HessianProxyFactory(); HessianClientInteface hc = (HessianClientInteface)factory.create(HessianClientInteface.class,url); System.out.println("hello: " + hc.hello()); System.out.println("name: " + hc.getUser().getPeopleName()); System.out.println("password: " + hc.getUser().getPeoplePassword()); hc.say("sssss"); System.out.println("say: " + hc.hello()); } }
3.测试结果
先启动tomcat服务,然后在运行客户端的程序,最后在控制台查看结果。例如,示例上所示,最后的结果如下:
Client:
hello: Hello Hessian
name: Apple
password: apple@1234
say: sssss
Server:
setting say success: sssss