一、简介
Hessian官网 http://hessian.caucho.com/
Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用了二进制协议,所以它很适合于发送二进制数据,Hessian主要作面向对象的消息通信。
实现暴露hessian接口只需4个步骤
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.
二、创建接口api
API for Basic service
package example; public interface Basic { public String hello();}
实现接口API
package example;
public class BasicService implements Basic {
private String _greeting = "Hello, world";
public void setGreeting(String greeting) {
_greeting = greeting;
}
public String hello() {
return _greeting;
}
}
三、在web.xml中,以servelet的形式暴露hessian接口
<web-app>
<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>example.BasicService</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>example.Basic</param-value>
</init-param>
</servlet>
<servlet-mapping>
<url-pattern>/hello</url-pattern>
<servlet-name>hello</servlet-name>
</servlet-mapping>
</web-app>
四、编写java调用客户端
public class HessianClient {
final static String url = "http://localhost:8080/hessian/remote/helloHessian";
public static void main(String[] args) throws Exception {
HessianProxyFactory factory = new HessianProxyFactory();
Basic basic = (Basic) factory.create(url);
System.out.println(basic.hello("jimi"));
}
}
启动hessian的工程后,运行hessian客户端