Hessian 理解

来源:互联网 发布:soap java开发实例 编辑:程序博客网 时间:2024/06/05 06:54

1.hessian的用途

 hessian的用处,是用来程序之间的通信的。与之相似的就是 WebService通信了。 但是两者是不一样的。 Webservice是基于SAOP的协议(简单理解为XML协议),而Hessian是基于二进制协议的。所以一对比就可以知道,Hessian效率更高。Hessian由于没有WSDL这种服务描述文件去对实现进行规定,似乎更适合内部分布式系统之间的交互,对外提供服务还是使用后两者更体面些。如果你的项目中,有好几个子系统,不同的系统中又有通信,用Hessian 是一个不错的选择。

2.实例

其实实例网上很多,也很简单,就随便网上COPY一个来,免得以后不记得了。1、在服务端的接口:
public interface IHello {    String sayHello();}

2、在服务端的实现类:

public class IHelloImpl extends HessianServlet implements IHello {    @Override    public String sayHello() {        // TODO Auto-generated method stub        return "Hello,I from HessianService";    }}

3.配置服务端WEB.xml(集成Spring)

<!-- 指定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>

这里可以配置多个servlet,需要hessian的使用一个特点的前缀,其他普通的请求使用另外的servlet。

4.Spring 中的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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://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>

这样配置完成之后,服务端的就算配置完成了。

5.将服务端的需要Hessian 的接口和关联的BEAN打成一个JAR包提供给客户端。
客户端集成好这个服务端提供的JAR包。(Hessian.jar也不能忘记了)
然后使用。

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-generated catch block            e.printStackTrace();        }    }}
1 0