<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更簡單、快捷。采用的是二進(jìn)制RPC協(xié)議,因為采用的是二進(jìn)制協(xié)議,所以它很適合于發(fā)送二進(jìn)制數(shù)據(jù)

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

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

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

    五、客戶端實現(xiàn)
    1、準(zhǔn)備工作
    (1)相關(guān)model,服務(wù)端的4個對象都需要加到客戶端來
    (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這個公共接口即可
    <?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ù)中測試,無需配置web.xml)
    3、客戶端調(diào)用測試類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();
            }


        }

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

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲情a成黄在线观看动漫尤物| 99久久这里只精品国产免费| 免费无码AV一区二区| 亚洲乱码国产乱码精华| 亚洲精品GV天堂无码男同| 中文字幕 亚洲 有码 在线| 亚洲AⅤ男人的天堂在线观看| 国产亚洲精品bv在线观看| 亚洲国产一级在线观看| 亚洲免费中文字幕| 免费无码不卡视频在线观看| 狠狠久久永久免费观看| 亚洲国产成人久久一区久久| 夜夜春亚洲嫩草影院| 亚洲成人免费电影| 亚洲中文字幕无码中文| 亚洲AV无码一区二区乱子仑| 在线免费观看h片| 免费人成在线视频| 国产中文在线亚洲精品官网| 亚洲网址在线观看你懂的| 亚洲综合久久精品无码色欲| 久久WWW免费人成—看片| 日韩免费观看一级毛片看看| 亚洲精品乱码久久久久66| 久久精品国产亚洲AV麻豆王友容| 亚洲精品乱码久久久久久久久久久久 | 3344在线看片免费| 日本zzzzwww大片免费| 在线观看免费成人| 亚洲色成人网站WWW永久| 亚洲日韩国产精品无码av| 亚洲欧美不卡高清在线| 国产一精品一AV一免费| 男人的好看免费观看在线视频| 亚洲黄黄黄网站在线观看| 亚洲无限乱码一二三四区| 成年大片免费视频播放一级| 免费观看黄色的网站| 亚洲日韩欧洲无码av夜夜摸| 亚洲人成图片网站|