ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks
          本文為原創,歡迎轉載,轉載請注明出處BlogJava。

          Hessian是一個輕量級的remoting onhttp工具,使用簡單的方法提供了RMI的功能. 相比WebService,Hessian更簡單、快捷。
          采用的是二進制RPC協議,因為采用的是二進制協議,所以它很適合于發送二進制數據。

          本文將分為以下幾個內容:
          一、一個簡單的Hessian調用例子
          二、Spring+Hessian的例子
          三、使我們的調用變得通用

          第一部分:一個簡單的Hessian調用例子
          建立web工程HessianService。
          這里為了簡單起見,我將遠程接口類、接口實現類都定義在此web工程下。

          個人覺得一個比較好的做法是將web工程分開,此web工程只定義對外的接口并提供服務,而實現類新建一個Java工程存放。
          這樣web工程依賴此Java工程,且客戶端也依賴此Java工程。

          1) 遠程接口類 ServiceRemote.java

           

          package com.al;

          import java.util.Map;

          @SuppressWarnings(
          "unchecked")
          public interface ServiceRemote  {
              
          public Map callService(Map inputMap) ;
          }


          2) 實現類 Service.java

          package com.al;

          import java.util.HashMap;
          import java.util.Map;

          @SuppressWarnings(
          "unchecked")
          public class Service implements ServiceRemote {

              
          public Map callService(Map inputMap) {
                  
          if(inputMap == null{
                      inputMap 
          = new HashMap();
                  }

                  
          // do something
                  
          // 
                  inputMap.put("NAME""Hessian");
                  
          return inputMap;
              }

          }


          3) web.xml配置

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee 
           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          >
              
          <servlet>
                  
          <servlet-name>hessianService</servlet-name>
                  
          <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
                  
          <init-param>
                      
          <param-name>service-class</param-name>
                      
          <param-value>com.al.Service</param-value>
                  
          </init-param>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>hessianService</servlet-name>
                  
          <url-pattern>/hessianService</url-pattern>
              
          </servlet-mapping>
          </web-app>


          經過以上3步,hessian服務部署算是完成了,在tomcat下發布。

          4) 調用方代碼

          package com.ai.client;

          import com.al.ServiceRemote;
          import com.caucho.hessian.client.HessianProxyFactory;

          public class ClientTest {
              
          public static void main(String[] args) throws Exception {
                  String url 
          = "http://localhost:8080/HessianService/hessianService";
                  HessianProxyFactory factory 
          = new HessianProxyFactory();
                  ServiceRemote rmt 
          = (ServiceRemote) factory.create(ServiceRemote.class, url);
                  System.out.println(rmt.callService(
          null));
              }

          }


          執行代碼,結果如下:
          {NAME=Hessian}
          說明調用遠程代碼成功了。

          二、Spring+Hessian的例子
          1) web工程HessianService 的ServiceRemote 和Service類不變。對web.xml進行修改:

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          >
              
          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
              
          </context-param>
              
          <servlet>
                  
          <servlet-name>dispatcher</servlet-name>
                  
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>dispatcher</servlet-name>
                  
          <url-pattern>/remote/*</url-pattern>
              
          </servlet-mapping>
              
          <welcome-file-list>
                  
          <welcome-file>index.jsp</welcome-file>
              
          </welcome-file-list>
          </web-app>


          這里要注意的是:
          a)dispatcher-servlet.xml這個文件的命名。servlet配置為dispatcher,則此文件定義規則為****-servlet.xml.

          b)另外就是Spring和hessian的版本問題。
          spring版本是2.5.6,需要在此web工程下引入:spring-2.5.6.jar、spring-webmvc-2.5.6.jar、commons-logging-1.1.1.jar。
          hessian的版本是hessian-3.1.6.jar,hessian的低版本如 hessian-3.0.13和此Spring版本不合。(我試驗了是配置不成功的。)

          2) dispatcher-servlet.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
              
          <description>hessian server properties</description>
              
          <bean id="serviceImpl" class="com.al.Service" />
              
          <bean name="/service" class="org.springframework.remoting.caucho.HessianServiceExporter">
                  
          <property name="service">
                      
          <ref bean="serviceImpl" />
                  
          </property>
                  
          <property name="serviceInterface">
                      
          <value>com.al.ServiceRemote</value>
                  
          </property>
              
          </bean>
          </beans>


          3) 客戶端代碼可以保持原樣不變,把調用hessian服務的url變換一下即可:
          http://localhost:8080/HessianService/remote/service
          http://IP:port/發布工程名/web.xml中配置的url-pattern/****-servlet.xml bean的ID。

          三、使我們的調用變得通用 將在下一篇中介紹。

          posted on 2010-10-17 21:11 李 明 閱讀(2827) 評論(0)  編輯  收藏 所屬分類: J2EESpring

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 大同县| 商洛市| 宁强县| 宜春市| 舞钢市| 长治县| 遂宁市| 辽宁省| 佛冈县| 同德县| 新河县| 岳池县| 元阳县| 广东省| 昆明市| 石阡县| 那坡县| 永城市| 南阳市| 乐陵市| 新沂市| 裕民县| 玉树县| 宜良县| 芜湖县| 门头沟区| 绿春县| 衢州市| 台湾省| 全州县| 内丘县| 漳浦县| 县级市| 德惠市| 资兴市| 离岛区| 微博| 印江| 洪泽县| 开原市| 来安县|