Java Spring1.2 Remote Invoke HTTP Invoker

近日,一个项目涉及到系统间接口调用,考虑到系统间用的都是java, spring 技术,建议在可能情况下,与其它系统间接口协议采用Spring支持的HTTP Invoker协议(见http://lee79.iteye.com/blog/344322,http invoker 性能是最高的,webservice是性能最差的)


Spring的版本是基于1.2

 
使用方法:
客户端:
1、web.xml 中加载 spring 配置
    ...

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/*context.xml           
        </param-value>
    </context-param>
    ...


2、spring (agent-context.xml) 中定义
    ...
    <bean id="adFetcher" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl" value="${service.adfetch.url}"/>
        <property name="serviceInterface" value="com.test.adapter.AdFetcherIF"/>
        <property name="httpInvokerRequestExecutor">
            <ref local="httpInvokerRequestExecutor"/>
        </property>
    </bean>

     <bean id="cooperateService" class="com.test.client.service.CooperateService" init-method="init">
        <property name="adFetcher">
            <ref local="adFetcher"/>
        </property>
    </bean>

    <bean id="httpInvokerRequestExecutor"
          class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
        <property name="httpClient">
            <bean class="org.apache.commons.httpclient.HttpClient">               
                <property name="connectionTimeout" value="${service.connectionTimeout}"/>
                <property name="timeout" value="${service.timeout}"/>
            </bean>
        </property>
    </bean>
    ...

3、代码使用,就像调用本地一样,CooperateService.getAdInfoList
   
服务器端:
1、web.xml中发布
    ....
    <servlet>
        <servlet-name>remote</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/remote.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>remote</servlet-name>
        <url-pattern>/remote/*</url-pattern>
    </servlet-mapping>
    ...

2、spring 远程接口配置文件 remote.xml
    ... 定义
    !--服务器端接口暴露-->
    <bean name="/AdFetch" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service">
            <ref bean="adFetcher"/>
        </property>
        <property name="serviceInterface" value="com.test.adapter.AdFetcherIF"/>
    </bean>
    ...

3、测试服务器端是否可访问,启动后,在浏览器中输入:http://serverip:port/context/remote/AdFetch,如出现,
       java.io.EOFException
    java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
    java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
    则OK

 

4、TestCase:Test.test1()

其它:
客户端使用时,要部署服务器端接口相关的jar包,本便中就是AdFetcherIF.class、AdData.class

 

附件是一个DEMO(包含客户端、服务器端代码)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值