Spring httpinvoker

服务端:

在applicationContext.xml文件中加入

<!--  Httpinvoker 配置 --> 
<bean id="httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<bean class="com.vncn.service.impl.StoreServiceImpl" /> <!-- 对外开放的类 -->
</property>
<property name="serviceInterface" value="com.ylzp.service.StoreService" /><!--对外开放的接口-->
</bean>

在spring-servlet.xml文件中加入

<!--  配置httpinvoke --> 
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/vncn" <!-- 客户端调用路径 --> value-ref="httpService" <!-- 对应上图beanid -->/> 
</map>
</property>
</bean>

StoreServiceImpl.java

import ****;

@Service
public class StoreServiceImpl implements StoreService {


    @Override
    public List<HttpStore> getHttpStore() {
    List<HttpStore> httpStores = new ArrayList<HttpStore>();
    List<Store> stores = getStoreAvailable(null);
        for(Store s: stores){
        HttpStore httpStore = new HttpStore();
        httpStore.setAddress(s.getAddress());
        httpStore.setLat(s.getLat());
        httpStore.setLng(s.getLng());
        httpStore.setName(s.getName());
        httpStore.setPhone(s.getPhone());
        httpStore.setPic(s.getPic());
        httpStore.setStatus(s.getStatus());
        httpStores.add(httpStore);
        }
        return httpStores;
    }
}

调用端配置applicationContext.xml

<!-- httpinvoker 配置 -->
<bean  
    id="StoreService"  
    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
    <property  
        name="serviceUrl"  
        value="http://localhost/vncn" /> <!-- 对应服务端的spring-servlet.xml的entry key -->
    <property  
        name="serviceInterface"  
        value="com.vv.web.service.StoreService" /> <!-- 包含要调用服务端方法的interface -->
    <property  
        name="httpInvokerRequestExecutor">  
        <ref  
            bean="httpInvokerRequestExecutor" />  
    </property>  
    </bean> 
<bean

这样在程序中就可以通过id得到bean.在调用方法得到想要的信息了。

一些优化配置:

<bean
id="httpInvokerRequestExecutor"
class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property
name="httpClient">
<bean
class="org.apache.commons.httpclient.HttpClient">
<property
name="connectionTimeout"
value="2000" />
<property
name="timeout"
value="5000" />
<property
name="httpConnectionManager">
<ref
bean="multiThreadedHttpConnectionManager" />
</property>
</bean>
</property>
</bean>
<bean
id="multiThreadedHttpConnectionManager"
class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property
name="params">
<bean
class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<property
name="maxTotalConnections"
value="600" />
<property
name="defaultMaxConnectionsPerHost"
value="512" />
</bean>
</property>
</bean>

写的时候出了很多错,最后写好了又让改的通用点,发现我都忘了怎么整好的了。记录一下,欢迎各位攻城狮批评指正

你可能感兴趣的:(Spring httpinvoker)