Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI的功能.
Hessian其实很像web service,只不过它的协议不是SOAP,而是它自己规定的binary协议。Hessian的server端提供一个servlet基类,client端获得一个service接口(也就是stub)之后调用上面的方法,stub将方法调用marshal之后通过HTTP传到server,server借助reflection调用service方法。
相比
WebService
,
Hessian
更简单、快捷。采用的是二进制
RPC
协议,因为采用的是二进制协议,所以它很适合于发送二进制数据
JAVA服务器端必须具备以下几点:
- 包含Hessian的jar包
- 设计一个接口,用来给客户端调用
- 实现该接口的功能
- 配置web.xml,配好相应的servlet
- 对象必须实现Serializable 接口
- 对于复杂对像可以使用Map的方法传递
客户端必须具备以下几点:
- java客户端包含Hessian.jar的包。C#中引用hessianCSharp.dll
- 具有和服务器端结构一样的接口。包括命名空间都最好一样
- 利用HessianProxyFactory调用远程接口。
下面提供几个简单的例子:
1. 简单JAVA客户端例子:
服务器端
新建一个名EasyHession的webProject项目,将Hessian.jar放入web-inf下的lib中
创建接口:
package app.demo;
public interface BasicAPI {
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
实现接口:
package app.demo;
public class BasicService implements BasicAPI {
private String _greeting = “Hello, world”;
public void setGreeting(String greeting)
{
_greeting = greeting;
System.out.println(“set greeting success:”+_greeting);
}
public String hello()
{
return _greeting;
}
public User getUser() {
return new User(“prance”, “meshow”);
}
}
创建一个实现Serializable 的projo类也可以是Bean。
package app.demo;
import java.io.Serializable;
public class User implements Serializable {
String userName = “snoopy”;
String password = “showme”;
public User(String user, String pwd) {
this.userName = user;
this.password = pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
接下来是配置web.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.4″
xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>app.demo.BasicService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
为了测试可以在编辑一个简单的test.jsp
<%@ page import=”com.caucho.hessian.client.HessianProxyFactory,
app.demo.BasicAPI”%>
<%@page language=”java” %>
<%
HessianProxyFactory factory = new HessianProxyFactory();
String url = (“http://” +request.getServerName() + “:” + request.getServerPort() +
request.getContextPath() + “/hello”);
out.println(url);
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
out.println(“Hello: ” + basic.hello());
out.println(“Hello: ” + basic.getUser() .getUserName() );
out.println(“Hello: ” + basic.getUser().getPassword() );
%>
将一切配置成功后,将项目发布到web 服务器上去,可以是Tomcat,Rain等等。
进入
http://localhost:8080/EasyHessian/test.jsp
结果如下:
http://localhost:8080/Hessian/hello
Hello: Hello, world
Hello: snoopy
Hello: showme
说明服务器端已经编写成功。
java客户端:
创建一个名为HessianClient的JAVAProject,载入hessian.jar包
创建与服务器端一样的接口,及基础类
public interface BasicAPI {
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
import java.io.Serializable;
public class User implements Serializable {
String userName = “snoopy”;
String password = “showme”;
public User(String user, String pwd) {
this.userName = user;
this.password = pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
创建客户端程序:
import com.caucho.hessian.client.HessianProxyFactory;
public class BasicClient {
public static void main(String[] args)
throws Exception
{
String url = “http://127.0.0.1:8080/Hessian/hello”;
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
System.out.println(“Hello: ” + basic.hello());
System.out.println(“Hello: ” + basic.getUser().getUserName());
System.out.println(“Hello: ” + basic.getUser().getPassword());
basic.setGreeting(“Hello Greeting”);
System.out.println(“Hello: ” + basic.hello());
}
}
运行结果:
Hello: Hello, world
Hello: prance
Hello: meshow
Hello: Hello Greeting
C#.net客户端:
创建一个C#客户端控制台程序,名为HessianClient,添加以引用,引入hessianCSharp.dll.
创建base.cs
加入代码
using System;
using System.Collections.Generic;
using System.Text;
namespace app.demo
{
public interface BasicAPI
{
void setGreeting(String greeting);
String hello();
User getUser();
}
public class User {
private String userName = “snoopy”;
private String password = “showme”;
public string UserName
{
get { return userName;}
set { userName = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
}
在program.cs中加入以下代码:
using System;
using System.Collections.Generic;
using System.Text;
using hessiancsharp.client;
using app.demo;
namespace HessianClient
{
class Program
{
static void Main(string[] args)
{
CHessianProxyFactory factory = new CHessianProxyFactory();
string url = “http://127.0.0.1:8080/Hessian/hello”;
BasicAPI basic = (BasicAPI)factory.Create(typeof(BasicAPI), url);
Console.WriteLine(“Hello: ” + basic.hello());
Console.WriteLine(“Hello: ” + basic.getUser().UserName);
Console.WriteLine(“Hello: ” + basic.getUser().Password);
basic.setGreeting(“Hello Greeting”);
Console.WriteLine(“Hello: ” + basic.hello());
}
}
}
运行结果:
客户端:
Hello: Hello, world
Hello: prance
Hello: meshow
Hello: Hello Greeting
服务器:
set greeting success:Hello Greeting