一、首先先说Hessian是什么?
Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用了二进制协议,所以它很适合于发送二进制数据,Hessian主要作面向对象的消息通信。Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hessian是使用自己的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,可以穿透防火墙,在这里不得不说一下RMI:RMI是一组用户开发分布式应用程序的API。他使用的是java序列化机制实现调用及返回值的编组于反编组。它使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。他可以被看做是RPC的Java版本,因为传统的RPC并不能很好的应用于分布式对象系统。而Java RMI则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。他也有它的缺点,他只能通过RMI协议来进行访问无法通过HTTP协议访问,无法穿透防火墙。
Hessian:写一个Hessian需要注意的问题:
1、JAVA服务器端必须具备以下几点:
包含Hessian的jar包
设计一个接口,用来给客户端调用
实现该接口的动能
配置web.xml,配置相应的servlet
对象必须实现Serializable接口
对于复杂对象可以使用Map的方法传递
2、客户端必须具备以下几点:
java客户端包含Hessian.jar包
Hessian:写一个Hessian需要注意的问题:
服务器端:
remote-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<bean name="/userInfo" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service">
<ref bean="userInfoService"/>
</property>
<property name="serviceInterface">
<value>com.allimu.remote.UserInfoService</value>
</property>
</bean>
</beans>
web.xml 加上
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
接口
com.allimu.remote.UserInfoService
实现
com.allimu.remote.impl.UserInfoServiceImpl
客户端
application-context.xml 加上
<bean id="userInfoService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>
http://localhost:8080/imu-server/remote/userInfo
</value>
</property>
<property name="serviceInterface">
<value>com.allimu.remote.UserInfoService</value>
</property>
</bean>
接口
com.allimu.remote.UserInfoService