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

          Hessian是一個(gè)輕量級(jí)的remoting onhttp工具,使用簡(jiǎn)單的方法提供了RMI的功能. 相比WebService,Hessian更簡(jiǎn)單、快捷。采用的是二進(jìn)制RPC協(xié)議,因?yàn)椴捎玫氖嵌M(jìn)制協(xié)議,所以它很適合于發(fā)送二進(jìn)制數(shù)據(jù)

          二、Hessian開發(fā)要點(diǎn)
          1、JAVA服務(wù)器端必須具備以下幾點(diǎn):

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

          三、基于Spring2.0的Hessian開發(fā)實(shí)例
          1、環(huán)境:
          (1)服務(wù)端:JDK1.4+weblogic8.1
          (2)客戶端:JDK1.6+weblogic11g
          2、相關(guān)JAR包:
          (1)服務(wù)端: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
          四、服務(wù)端的實(shí)現(xiàn)
          1、相關(guān)model(com.govdo.model)
          (1)ServiceRequest.java 客戶端請(qǐng)求服務(wù)類,用于接受客戶端請(qǐng)求數(shù)據(jù) 
          ServiceRequest.java
          (2)ServiceResponse.java 客戶端響應(yīng)服務(wù)類,用于返回客戶端響應(yīng)數(shù)據(jù)
          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 城市信息客戶端請(qǐng)求對(duì)象,實(shí)現(xiàn)Serializable,源碼略
          (4)QueryCityInfoOut.java 城市信息客戶端響應(yīng)對(duì)象,實(shí)現(xiàn)Serializable,源碼略
          2、用于客戶端調(diào)用的公共接口及實(shí)現(xiàn)
          (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 實(shí)現(xiàn)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的公共類,用于遠(yuǎn)程實(shí)現(xiàn)接口Action,其中serviceID是真正實(shí)現(xiàn)功能的bean,這個(gè)bean必須實(shí)現(xiàn)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、服務(wù)端的相關(guān)配置
          (1)remoting-servlet.xml 在WEB-INF下添加這個(gè)xml文件,其中/myHessian是暴露給客戶端的訪問路經(jīng),Action是調(diào)用接口,remotingAction是公共實(shí)現(xiàn)類
          <?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中增加相應(yīng)的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、增加具體實(shí)現(xiàn)功能的bean
          (1)HessianTestImpl.java具體實(shí)現(xiàn)類,繼承AbstractAction相當(dāng)于實(shí)現(xiàn)了Action接口,這里對(duì)請(qǐng)求數(shù)據(jù)做了簡(jiǎn)單處理,對(duì)城市名稱后加了(by remoting),然后返回客戶端,注意要實(shí)現(xiàn)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服務(wù)的XML配置文件中增加一個(gè)Bean(不妨就加在remoting-servlet.xml中,也可以另寫一個(gè)xml,但必須在web.xml中加載),后面客戶端實(shí)現(xiàn)會(huì)用到這個(gè)bean。
          <bean id="hessianTest" class="com.govdo.test.action.HessianTestImpl">
              
          </bean>
          到這里服務(wù)端實(shí)現(xiàn)已全部完成,打包部署完工。這里假設(shè)部署實(shí)例上下文路徑為:http://127.0.0.1:7008/webRoot/

          五、客戶端實(shí)現(xiàn)
          1、準(zhǔn)備工作
          (1)相關(guān)model,服務(wù)端的4個(gè)對(duì)象都需要加到客戶端來
          (2)客戶端調(diào)用的接口,Action接口也要加到客戶端來
          2、遠(yuǎn)程調(diào)用相關(guān)配置
          (1)remoting-client.xml 配置遠(yuǎn)程調(diào)用接口和URL,回顧上章3(1)節(jié),/myHessian是在remoting-servlet.xml 中配置的遠(yuǎn)程訪問服務(wù)路徑,所以這里URL為http://127.0.0.1:7008/webRoot/remoting/myHessian,而接口則只需配Action這個(gè)公共接口即可
          <?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函數(shù)中測(cè)試,無需配置web.xml)
          3、客戶端調(diào)用測(cè)試類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();
                  }


              }

          }
          運(yùn)行結(jié)果:CityId:Shenzhen;CityName:深圳(by remoting)
          這個(gè)(by remoting)就是通過服務(wù)端程序加的,請(qǐng)回顧上章4(1)節(jié),至此客戶端調(diào)用實(shí)現(xiàn)結(jié)束。

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 邵阳县| 长兴县| 竹山县| 女性| 静海县| 栾城县| 宁阳县| 新源县| 昌黎县| 吉安县| 洛浦县| 大连市| 郁南县| 昭通市| 朝阳区| 黎城县| 河北区| 临泽县| 北流市| 封开县| 明水县| 甘南县| 抚松县| 繁峙县| 灵川县| 元氏县| 商城县| 曲沃县| 淮阳县| 巍山| 洪湖市| 阿拉尔市| 海淀区| 清丰县| 甘谷县| 寿光市| 津市市| 九龙县| 涿鹿县| 多伦县| 枣强县|