原文点这里。
官网点这里。
Hession是什么
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,实现面向对象的消息通信,相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,适合于发送二进制数据。
一般的RMI是一组用户开发分布式应用程序的API,其远程调用和返回值采用java自身的序列化机制,通过Java远程方法协议(Java Remote Method Protocol)通信;可以被看做是RPC的Java版本。其缺点是:只能通过RMI协议进行通信,无法通过HTTP协议访问,无法穿透防火墙。
Hessian的初衷就是支持动态类型,格式紧凑。跨语言Hessian使用自己的序列化机制,但其支持的数据类型是有限制的,不支持复杂的对象,可以穿透防火墙。
其他类似工具,见这里。
注意点
JAVA服务器端必须具备以下几点:
1.包含Hessian的jar包
2.设计一个接口,用来给客户端调用
3.实现该接口的动能
4.配置web.xml,配置相应的servlet
5.对象必须实现Serializable接口
6.对于复杂对象可以使用Map的方法传递
客户端必须具备以下几点:
1.java客户端包含Hessian.jar包
2.具有和服务器端结构一样的接口和实体类。包括命名空间都最好一样。利用HessianProxyFactory调用远程接口。
举例
一般使用
1、在服务端的接口:
public interface IHello {
String sayHello();
}
2
、在服务端的实现类:
public class IHelloImpl extends HessianServlet implements IHello {
@Override
public String sayHello() {
return "Hello,I fromHessianService";
}
}
3
、在客户端的类:
public class ClientTest {
public static String url ="http://127.0.0.1:8080/HessianService/Hello";
public static void main(String[] args){
HessianProxyFactory factory = new HessianProxyFactory();
try {
IHello iHello = (IHello)factory.create(IHello.class, url);
System.out.println(iHello.sayHello());
} catch(MalformedURLException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
}
4
、先将服务器端的类
link
到客户端
,
或者是将服务器端打包放到客户端,
5 、在 web.xml 中进行配置:
服务器端:
<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.kcpt.hessian.service.IHelloImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.kcpt.hessian.service.IHello</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
执行成功:输出Hello,I from HessianService,这是简单的hessian实现方式,看起来比较简单
hessian与spring结合.
在实际应用中,我们不只是简单的只使用hessian来进行通信的,如果方法多得话,还不如直接写在客户端来调用,然而:当hessian与spring结合后,大大减少了这些操作,将dao层的操作全部放在hessian服务端,将业务逻辑全部放在hessian客户端,这样的话我们的hessian客户端和服务端完全分离,因此我们的业务逻辑和dao层就真正的达到了分离,就可以放在不同的服务器上,当然hessian的通信的作用不仅仅只有这些。接口和实现和上边的一样:只是在web.xml中配置比较麻烦:
例子:
1 、服务器端:增加 remoting-servlet.xml 配置文件:用来配置 bean ,并将 bean 导出为 hessian 服务:
<?xml version ="1.0" encoding = "UTF-8" ?>
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<!-- 定义普通的bean实例 -->
<bean id="Hello" class="com.kcpt.hessian.service.IHelloImpl"/>
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
<bean name="/remoting"class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean-->
<property name="service"ref="Hello"/>
<!-- Hessian服务的接口-->
<property name="serviceInterface"value="com.kcpt.hessian.service.IHello"/>
</bean>
</beans>
2
、
web.xml
文件的配置:
首先是监听器:spring的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--添加监听器 -->
</listener>
<!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/remoting-servlet.xml</param-value>
</context-param>
<!-- Hessian通过Servlet提供远程服务,需要将某个匹配的模式映射到hessian服务中,spring的dispatcherServlet能完成此功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。而HessianServiceExporter提供bean服务。 -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
3
、在客户端:
同样要加spring监听器和context-param指定bean的文件
声明bean的xml文件:
<?xml version ="1.0" encoding = "UTF-8" ?>
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"> //hessian的地址和名称请求转发的名称
<value>http://127.0.0.1:8080/HessianService/remoting</value>
</property>
<property name="serviceInterface"> //hessian所要调用的接口
<value>com.kcpt.hessian.service.IHello</value>
</property>
</bean>
</beans>
4 、客户端的程序中要写:
ApplicationContextcontext = new ClassPathXmlApplicationContext("com/kcpt/hessian/client/remoting-client.xml"); //这里只是你声明的bean的xml文件所在的路径
IHello b = (IHello)context.getBean("myServiceClient");
来获取到ihello这个接口,从而就能够调用这个接口里的方法
参考:
http://blog.csdn.net/anerou/article/details/6715584
http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html