Java Hessian小试(转)

来源:互联网 发布:matlab矩阵绝对值 编辑:程序博客网 时间:2024/04/30 15:34

http://zhuchengzzcc.iteye.com/blog/1534311

 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议(Binary),因为采用的是二进制协议,所以它很适合于发送二进制数据。Hessian通常通过Web应用来提供服务,因此非常类似于WebService。只是它不使用SOAP协议。 
      Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类, 用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,,推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。

 

Hessian处理过程示意图: 
客户端——>序列化写到输出流——>远程方法(服务器端)——>序列化写到输出流 ——>客户端读取输入流——>输出结果

环境搭建 
Hessian的下载和安装请按如下步骤进行: 
(1)登http://www.caucho.com/hessian/下载Hessian。 
(2)把Hessian相应的Jar包放到Web应用下,所有的jar文件都应该放在WEB-INF/lib下,该文件也不例外。

 

 

两种方式 
1.纯Hessian 
这种方式主要是适用于工程中没有适用像spring框架的情况下,好处是配置方便,但是当内容多的情况下,配置的内容很多。

 

 

Java代码  收藏代码
  1. package test;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * @author zhuc 
  7.  * @version 2012-5-17 下午9:15:50 
  8.  */  
  9. public interface IService {  
  10.     /** 
  11.      * @param name 
  12.      * @return 
  13.      */  
  14.     public String sayHello(String name);  
  15.       
  16.     /** 
  17.      * @return 
  18.      */  
  19.     public Car getMyCar();  
  20.       
  21.     /** 
  22.      * @return 
  23.      */  
  24.     public List<String> getList();  
  25. }  
  26.   
  27.   
  28. package test;  
  29.   
  30. import java.util.ArrayList;  
  31. import java.util.List;  
  32.   
  33. import com.caucho.hessian.server.HessianServlet;  
  34.   
  35. /** 
  36.  * @author zhuc 
  37.  * @version 2012-5-17 下午9:16:35 
  38.  */  
  39. public class ServiceImpl extends HessianServlet implements IService {  
  40.   
  41.     /** 
  42.      *  
  43.      */  
  44.     private static final long serialVersionUID = 8385639368192939451L;  
  45.   
  46.     @Override  
  47.     public String sayHello(String name) {  
  48.         return "hello: " + name;  
  49.     }  
  50.   
  51.     @Override  
  52.     public Car getMyCar() {  
  53.         Car c = new Car();  
  54.         c.setName("哈哈车");  
  55.         return c;  
  56.     }  
  57.   
  58.     @Override  
  59.     public List<String> getList() {  
  60.         List<String> list = new ArrayList<String>();  
  61.         list.add("haha");  
  62.         list.add("hehe");  
  63.         return list;  
  64.     }  
  65.   
  66. }  
  67.   
  68.   
  69. package test;  
  70.   
  71. import java.io.Serializable;  
  72.   
  73. /** 
  74.  * @author zhuc 
  75.  * @version 2012-5-17 下午10:29:18 
  76.  */  
  77. public class Car implements Serializable {  
  78.   
  79.     /** 
  80.      *  
  81.      */  
  82.     private static final long serialVersionUID = -1115598660168001267L;  
  83.   
  84.     private String name;  
  85.   
  86.     /** 
  87.      * @return the name 
  88.      */  
  89.     public String getName() {  
  90.         return name;  
  91.     }  
  92.   
  93.     /** 
  94.      * @param name the name to set 
  95.      */  
  96.     public void setName(String name) {  
  97.         this.name = name;  
  98.     }  
  99.   
  100.     /** 
  101.      * @return the serialversionuid 
  102.      */  
  103.     public static long getSerialversionuid() {  
  104.         return serialVersionUID;  
  105.     }  
  106.       
  107.       
  108. }  

 

 

web.xml的详细配置

Xml代码  收藏代码
  1. <servlet>  
  2.         <servlet-name>Hello</servlet-name>  
  3.         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>  
  4.         <init-param>  
  5.             <param-name>home-class</param-name>  
  6.             <param-value>test.ServiceImpl</param-value>  
  7.         </init-param>  
  8.         <init-param>  
  9.             <param-name>home-api</param-name>  
  10.             <param-value>test.IService</param-value>  
  11.         </init-param>  
  12.         <load-on-startup>1</load-on-startup>  
  13.     </servlet>  
  14.   
  15.     <servlet-mapping>  
  16.         <servlet-name>Hello</servlet-name>  
  17.         <url-pattern>/Hello</url-pattern>  
  18.     </servlet-mapping>  

 

客户端远程调用服务器端提供的接口,利用的就是Hessian的HessianProxyFactory,来实现远程代理

 

Java代码  收藏代码
  1. package test;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.util.List;  
  5.   
  6. import com.caucho.hessian.client.HessianProxyFactory;  
  7.   
  8. /** 
  9.  * @author zhuc 
  10.  * @version 2012-5-17 下午9:31:14 
  11.  */  
  12. public class ClientTest {  
  13.   
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         String url = "http://localhost:8080/Hessian/Hello";  
  19.         HessianProxyFactory factory = new HessianProxyFactory();  
  20.         try {  
  21.             IService hello = (IService) factory.create(IService.class, url);  
  22.             System.out.println(hello.sayHello("zhuc-no"));  
  23.   
  24.             Car c = hello.getMyCar();  
  25.             System.out.println(c.getName());  
  26.   
  27.             List<String> list = hello.getList();  
  28.             for (String string : list) {  
  29.                 System.out.println(string);  
  30.             }  
  31.         } catch (MalformedURLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.   
  35.     }  
  36.   
  37. }  
 

 

输出结果,成功

hello: zhuc-no
 哈哈车
 haha
 hehe

 

 

2.Hessian与Spring整合 
相比上一种方式,这个方式就有点麻烦了。Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。而HessianServiceExporter提供bean服务。 
所以Hessian与Spring整合主要就是一下两个工作: 
1:通过DispatcherServlet来拦截URL请求。 
2:HessianServiceExporter提供bean服务,Spring使用HessianServiceExporter,将一个常规bean导出成Hessian服务。 

 

修改web.xml的详细配置 

 

Xml代码  收藏代码
  1. <servlet>  
  2.         <servlet-name>remoting</servlet-name>  
  3.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.         <!--     
  5.         <init-param>    
  6.             <param-name>contextConfigLocation</param-name>    
  7.             <param-value>/WEB-INF/remoting-servlet.xml</param-value>    
  8.         </init-param>    
  9.           
  10.          在/WEB-INF/增加一个叫servlet-name-servlet.xml配置文件。该文件的命名有一定的规则,  
  11.          红色servlet-name需要和web.xml中的<servlet-name>springhessian</servlet-name>定义的名称相匹配,  
  12.          比如本例子应叫remoting-servlet.xml,这样简单也不容易出错。  
  13.            
  14.         当然该文件也可以自己任意命名。如果是自定义的文件名称不符合上述默认的规则,  
  15.         需要在<servlet>中增加<init-param>相关属性,人为指定加载配置文件,否则会报错  
  16.          -->   
  17.         <load-on-startup>1</load-on-startup>  
  18.     </servlet>  
  19.     <servlet-mapping>  
  20.         <servlet-name>remoting</servlet-name>  
  21.         <url-pattern>/remoting/*</url-pattern>  
  22.     </servlet-mapping>  

 

 

配置remoting-servlet.xml文件,将其放于/WEB-INF/下

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  11.   
  12.     <!-- 定义普通bean实例 -->  
  13.     <bean id="serviceImpl" class="test.ServiceImpl" />  
  14.   
  15.     <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->  
  16.     <bean name="/hessianCommentService"  
  17.         class="org.springframework.remoting.caucho.HessianServiceExporter">  
  18.         <!-- 需要导出的目标bean -->  
  19.         <property name="service" ref="serviceImpl" />  
  20.         <!-- Hessian服务的接口 -->  
  21.         <property name="serviceInterface" value="test.IService" />  
  22.     </bean>  
  23. </beans>  

 

客户端定义一个remoting-client.xml文件

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  11.   
  12.     <bean id="myServiceClient"  
  13.         class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  14.         <property name="serviceUrl">  
  15.             <value>http://localhost:8080/Hessian/remoting/hessianCommentService</value>  
  16.         </property>  
  17.         <property name="serviceInterface">  
  18.             <value>test.IService</value>  
  19.         </property>  
  20.     </bean>  
  21.   
  22. </beans>  

 

 

客户端调用

 

 

Java代码  收藏代码
  1. package spring;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import test.Car;  
  9. import test.IService;  
  10.   
  11. /** 
  12.  * @author zhuc 
  13.  * @version 2012-5-17 下午9:59:31 
  14.  */  
  15. public class ClientTest {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) {  
  21.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  22.                 "spring/remoting-client.xml");  
  23.         IService hello = (IService) context.getBean("myServiceClient");  
  24.         System.out.println(hello.sayHello("zhuc-spring"));  
  25.   
  26.         Car c = hello.getMyCar();  
  27.         System.out.println(c.getName());  
  28.   
  29.         List<String> list = hello.getList();  
  30.         for (String string : list) {  
  31.             System.out.println(string);  
  32.         }  
  33.     }  
  34.   
  35. }  

 

 

输出结果同上。

 

注:上述spring方式,客户端中的请求URL

http://localhost:8080/Hessian/remoting/hessianCommentService 中的hessianCommentService需要和remoting-servlet.xml配置中HessianServiceExporter的bean name相同,否则将会找不到server端


0 0
原创粉丝点击