<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(1293) 評論(1)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 热99re久久精品精品免费| 亚洲成A人片在线观看中文| 亚洲国产精品无码久久| 亚洲国产精品一区二区九九| 在线观看肉片AV网站免费| 亚洲人成人77777网站不卡 | 永久免费无码网站在线观看| 美女啪啪网站又黄又免费| 亚洲精品国产精品乱码视色| 久久经典免费视频| 一级毛片a免费播放王色电影 | 亚洲色图.com| 四虎影视在线永久免费观看| 免费黄网站在线看| 亚洲精品久久无码| 亚洲人成网站在线播放影院在线| 亚洲日韩在线中文字幕第一页| 又爽又黄无遮挡高清免费视频| 免费人成视频在线观看不卡| 一级人做人爰a全过程免费视频| 亚洲人成无码网站| 毛片a级毛片免费观看免下载| 一级有奶水毛片免费看| 中文字幕 亚洲 有码 在线| 伊人久久综在合线亚洲91| 成人A级毛片免费观看AV网站| 天黑黑影院在线观看视频高清免费 | 日韩版码免费福利视频| 中国好声音第二季免费播放| av无码东京热亚洲男人的天堂| 亚洲精品国产日韩| 亚洲av永久无码精品网站| 国产免费私拍一区二区三区| h片在线免费观看| 美女视频黄的免费视频网页 | 国产精品视频全国免费观看| 亚洲第一综合天堂另类专| 亚洲综合亚洲国产尤物| 亚洲精品tv久久久久久久久| 亚洲国产成人VA在线观看| 免费观看男人免费桶女人视频|