posts - 1,  comments - 1,  trackbacks - 0
          一、Hessian簡介(摘自百度百科)

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

          二、Hessian開發要點
          1、JAVA服務器端必須具備以下幾點:

          (1)包含Hessian的jar包
          (2)設計一個接口,用來給客戶端調用
          (3)該接口的實現類
          (4)配置web.xml中的servlet
          (5)對象必須實現Serializable 接口
          (6)對于復雜對象可以使用Map的方法傳遞
          7)不支持JDK1.6+weblogic11g+Spring
          -2、客戶端必須具備以下幾點:
          (1)包含Hessian的jar包。
          (2)具有和服務器端結構一樣的接口和交互對象。
          (3)利用HessianProxyFactory調用遠程接口。

          三、基于Spring2.0的Hessian開發實例
          1、環境:
          (1)服務端:JDK1.4+weblogic8.1
          (2)客戶端:JDK1.6+weblogic11g
          2、相關JAR包:
          (1)服務端:spring.jar、hessian-2.1.12.jar、commons-logging-1.0.4.jar
          (2)客戶端:spring.jar、hessian-2.1.12.jar、commons-logging-1.0.4.jar
          四、服務端的實現
          1、相關model(com.govdo.model)
          (1)ServiceRequest.java 客戶端請求服務類,用于接受客戶端請求數據 
          ServiceRequest.java
          (2)ServiceResponse.java 客戶端響應服務類,用于返回客戶端響應數據
          package com.govdo.model;

          import java.io.Serializable;
          import java.util.Map;

          public class ServiceResponse implements Serializable {

              
          private static final long serialVersionUID = 1L;
              
          public static final String SERVICE_RESPONSE_RESULT = "SERVICE_RESPONSE_RESULT";
              
          public static final String BUSINESS_SUCCESS = "0";
              
          public static final String BUSINESS_FAILURE = "1";

              
          private Map model = null;

              
          public ServiceResponse() {}
              
          public ServiceResponse(Map model) {
                  
          this.model = model;
              }

              
          public Map getModel() {
                  
          return model;
              }


              
          public void setModel(Map model) {
                  
          this.model = model;
              }


          }
          (3)QueryCityInfoIn.java 城市信息客戶端請求對象,實現Serializable,源碼略
          (4)QueryCityInfoOut.java 城市信息客戶端響應對象,實現Serializable,源碼略
          2、用于客戶端調用的公共接口及實現
          (1)Action.java 暴露給客戶端的抽象公共接口
          package com.govdo.action;

          import com.govdo.model.ServiceRequest;
          import com.govdo.model.ServiceResponse;

          public abstract interface Action {

              
          public abstract ServiceResponse perform(ServiceRequest request) throws Exception;
          }
          (2)AbstractAction.java 實現Action、BeanFactoryAware接口的抽象類
          package com.govdo.action;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;

          import com.govdo.model.ServiceRequest;
          import com.govdo.model.ServiceResponse;

          public abstract class AbstractAction implements Action, BeanFactoryAware {

              
          protected BeanFactory context;
              
          public void setBeanFactory(BeanFactory context) throws BeansException {
                  
          this.context = context;
              }


              
          public abstract ServiceResponse perform(ServiceRequest request)
                      
          throws Exception;
          }
          (3)RemotingAction 繼承AbstractAction的公共類,用于遠程實現接口Action,其中serviceID是真正實現功能的bean,這個bean必須實現perform方法
          package com.govdo.action;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import com.govdo.model.ServiceRequest;
          import com.govdo.model.ServiceResponse;
          import com.govdo.util.StringUtils;

          public class RemotingAction extends AbstractAction {

              
          private Log logger = LogFactory.getLog(getClass());
              
          public ServiceResponse perform(ServiceRequest request)
                      
          throws Exception {
                  String serviceID 
          = (String)request.getRequestedServiceID();
                  
          if (StringUtils.isBlank(serviceID)) {
                      logger.error(
          "service id isn't allowed to be null!");
                      
          throw new IllegalArgumentException("service id isn't allowed to be null!");
                  }

                  Action action 
          = (Action)context.getBean(serviceID);
                  
          return action.perform(request);
              }

          }
          3、服務端的相關配置
          (1)remoting-servlet.xml 在WEB-INF下添加這個xml文件,其中/myHessian是暴露給客戶端的訪問路經,Action是調用接口,remotingAction是公共實現類
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
              "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              <bean id="remotingAction" class="com.govdo.action.RemotingAction">
              
          </bean>
              
          <bean name="/myHessian"
                  class
          ="org.springframework.remoting.caucho.HessianServiceExporter">
                  
          <property name="service" ref="remotingAction" />
                  
          <property name="serviceInterface" value="com.govdo.action.Action" />
              
          </bean>
          </beans>
          (2)web.xml中增加相應的servlet配置,這里采用Spring的org.springframework.web.servlet.DispatcherServlet
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee
              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          >

              
          <welcome-file-list>
                  
          <welcome-file>index.jsp</welcome-file>
              
          </welcome-file-list>

              
          <servlet>
                  
          <servlet-name>remoting</servlet-name>
                  
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  
          <init-param>
                      
          <param-name>contextConfigLocation</param-name>
                      
          <param-value>
                      ,/WEB-INF/remoting-servlet.xml
                      
          </param-value>
                  
          </init-param>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>

              
          <servlet-mapping>
                  
          <servlet-name>remoting</servlet-name>
                  
          <url-pattern>/remoting/*</url-pattern>
              
          </servlet-mapping>

          </web-app>
          4、增加具體實現功能的bean
          (1)HessianTestImpl.java具體實現類,繼承AbstractAction相當于實現了Action接口,這里對請求數據做了簡單處理,對城市名稱后加了(by remoting),然后返回客戶端,注意要實現perform方法
          package com.govdo.test.action;

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

          import org.springframework.beans.BeanUtils;

          import com.govdo.action.AbstractAction;
          import com.govdo.model.QueryCityInfoIn;
          import com.govdo.model.QueryCityInfoOut;
          import com.govdo.model.ServiceRequest;
          import com.govdo.model.ServiceResponse;

          public class HessianTestImpl extends AbstractAction {

              
          public ServiceResponse perform(ServiceRequest request) throws Exception {

                  Map resultMap 
          = new HashMap();
                  QueryCityInfoIn in 
          = (QueryCityInfoIn)request.getCurrentRequestObject();
                  QueryCityInfoOut out 
          = new QueryCityInfoOut();
                  BeanUtils.copyProperties(in, out);
                  out.setCityName(out.getCityName()
          +"(by remoting)");
                  resultMap.put(ServiceResponse.BUSINESS_SUCCESS, out);
                  Map model 
          = new HashMap();
                  model.put(ServiceResponse.SERVICE_RESPONSE_RESULT, resultMap);
                  
          return new ServiceResponse(model);
              }

          }
          (2)在Spring提供bean服務的XML配置文件中增加一個Bean(不妨就加在remoting-servlet.xml中,也可以另寫一個xml,但必須在web.xml中加載),后面客戶端實現會用到這個bean。
          <bean id="hessianTest" class="com.govdo.test.action.HessianTestImpl">
              
          </bean>
          到這里服務端實現已全部完成,打包部署完工。這里假設部署實例上下文路徑為:http://127.0.0.1:7008/webRoot/

          五、客戶端實現
          1、準備工作
          (1)相關model,服務端的4個對象都需要加到客戶端來
          (2)客戶端調用的接口,Action接口也要加到客戶端來
          2、遠程調用相關配置
          (1)remoting-client.xml 配置遠程調用接口和URL,回顧上章3(1)節,/myHessian是在remoting-servlet.xml 中配置的遠程訪問服務路徑,所以這里URL為http://127.0.0.1:7008/webRoot/remoting/myHessian,而接口則只需配Action這個公共接口即可
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
              "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
              
          <bean id="hessianTestClient"
                  class
          ="org.springframework.remoting.caucho.HessianProxyFactoryBean">
                  
          <property name="serviceUrl"
                      value
          ="http://127.0.0.1:7008/webRoot/remoting/myHessian" />
                  
          <property name="serviceInterface" value="com.govdo.action.Action" />
              
          </bean>
          </beans>
          (2)web.xml中加載remoting-client.xml (如果是在main函數中測試,無需配置web.xml)
          3、客戶端調用測試類TestHessianTest.java
          package com.govdo.test.action;

          import java.util.Map;

          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import com.govdo.action.Action;
          import com.govdo.model.QueryCityInfoIn;
          import com.govdo.model.QueryCityInfoOut;
          import com.govdo.model.ServiceRequest;
          import com.govdo.model.ServiceResponse;

          public class TestHessianTest {

              
          public static void main(String[] args) {
                  ClassPathXmlApplicationContext context 
          = new ClassPathXmlApplicationContext("remoting-client.xml");
                  
          try {
                      QueryCityInfoIn in 
          = new QueryCityInfoIn();
                      in.setCityId(
          "Shenzhen");
                      in.setCityName(
          "深圳");
                      Action ac 
          = (Action)context.getBean("hessianTestClient");
                      ServiceRequest serviceRequest 
          = new ServiceRequest();
                      serviceRequest.setRequestedServiceID(
          "hessianTest");
                      serviceRequest.setCurrentRequestObject(in);
                      ServiceResponse serviceResponse 
          = ac.perform(serviceRequest);
                      Map model 
          = (Map)serviceResponse.getModel();
               
                 if (model != null{
                          Map resultList 
          = (Map)(model.get(ServiceResponse.SERVICE_RESPONSE_RESULT));
                          QueryCityInfoOut out 
          = (QueryCityInfoOut)(resultList.get(ServiceResponse.BUSINESS_SUCCESS));
                          System.out.println(
          "CityId:"+out.getCityId()+";CityName:"+out.getCityName());
                      }

                  }
           catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }


              }

          }
          運行結果:CityId:Shenzhen;CityName:深圳(by remoting)
          這個(by remoting)就是通過服務端程序加的,請回顧上章4(1)節,至此客戶端調用實現結束。

          六、小結
          對于服務端的這種實現方式,有以下優缺點
          1、優點
          (1)配置簡單擴展性好,服務端只需一次配置,客戶端即可調用多個實現不同功能的實例,如文章例中的hessianTest是通過serviceRequest.setRequestedServiceID("hessianTest")去獲取,也就是說服務端的其他實現了Action接口的bean都可以通過這個方法遠程獲取并調用
          2、缺點
          (1)目前經本人測試,hessian的多個版本(包括最老的最新的)都不支持JDK1.6+weblogic11g的Spring配置,而weblogic11g需要依賴JDK1.6以上版本,weblogic8.1不支持1.4以上版本,所以服務端想用泛型就不可能了,本人對泛型情有獨鐘,本打算出一個Spring+hessian+ibatis的介紹,看來只能分開講了。
          posted on 2011-07-11 21:55 Ciber 閱讀(1291) 評論(1)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 东明县| 雅安市| 乡宁县| 合作市| 泽库县| 文化| 梧州市| 黎城县| 临邑县| 若尔盖县| 拜城县| 体育| 吕梁市| 桃园市| 伊春市| 明光市| 嵊泗县| 航空| 腾冲县| 金阳县| 长宁县| 濮阳市| 安吉县| 岚皋县| 碌曲县| 婺源县| 桃园县| 邵阳县| 余姚市| 新邵县| 二连浩特市| 昌乐县| 昆明市| 清河县| 松阳县| 永康市| 武宁县| 许昌市| 十堰市| 阳信县| 尤溪县|