您的位置:首页 > 理论基础 > 计算机网络

[转载]spring的httpInvoker远程调用 示例

2012-07-18 11:09 288 查看
转自/article/4017841.html

服务端

实体类 注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!

public class User implements Serializable {

private static final long serialVersionUID = -2270657445785947163L;

private String userName;

private int age;

public User(String userName,int age){
this.userName = userName;
this.age = age;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String toString(){
return "my name is:"+userName+"and my age is:"+age;
}
}


接口

public interface TestHttpInvokerInterface {

User getUser(String userName,int age);
}


实现类

public class TestHttpInvokerInterfaceImpl implements TestHttpInvokerInterface {
private Logger logger = Logger.getLogger(TestHttpInvokerInterfaceImpl.class);

public User getUser(String userName, int age) {
User user = new User(userName,age);
logger.debug(user);
return user;
}

}


web.xml

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/service-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping>


service-config.xml  HttpInvokerServiceExporter,这个类用来在服务器端包装需要暴露的接口。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<bean id="userService"  class="com.wyq.service.impl.TestHttpInvokerInterfaceImpl" />

<bean id="serviceExporter"    class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="userService" />
<property name="serviceInterface" value="com.wyq.service.TestHttpInvokerInterface" />
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<!--          <map>
<entry  key="/user.service" value-ref="userService" />
</map>  -->
<props>
<prop key="/user.service">serviceExporter</prop>
</props>
</property>
</bean>

</beans>


服务端设置完毕

客户端

定义一个相同的接口

定义一个测试方法

public class HttpInvokerClinet {

private static Logger logger = Logger.getLogger(HttpInvokerClinet.class);

public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/httpInvokerClinet-config.xml");
TestHttpInvokerInterface service = (TestHttpInvokerInterface)context.getBean("userService");
User user = service.getUser("wyq", 25);
logger.debug(user);
}
}


定义httpInvokerClient-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置文件导入 -->
<bean id="configure"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="classpath:/config.properties" />
</bean>

<bean  id="userService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 服务端地址 -->
<property  name="serviceUrl"  value="${HttpInvokerServer}/WYQ/user.service" />
<property   name="serviceInterface"
value="com.wyq.service.TestHttpInvokerInterface" />
<!-- 若用默认可不定义此属性 -->
<property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor"></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="2000" />
<property  name="timeout"  value="5000" />
</bean>
</property>
</bean>-->

<bean  id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property name="httpClient" ref="httpclient" />
</bean>
<!--  HttpClient 启用Apache HttpClient 通信
默认实现,服务器平均10s左右才能响应一个请求。
多线程实现,服务器平均20ms左右响应一个请求。 -->
<bean id="httpclient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg>
<ref bean="connectionManager"/>
</constructor-arg>
</bean>

<!-- http管理参数配置 -->
<bean id="connectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property name="params" ref="connectionManagerParams"/>
</bean>

<!-- httpclient线程池 -->
<bean id="connectionManagerParams" class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<!-- 设置 连接超时时间(毫秒),默认为0不使用 -->
<property name="connectionTimeout" value="5000"/>
<!-- 设置 读取数据超时(毫秒),默认为0不使用 -->
<property name="soTimeout" value="10000"/>
<!-- 设置默认的连接数量最大允许对一个给定的主机配置 -->
<property name="maxTotalConnections" value="30"/>
<!-- 允许的最大连接数 -->
<property name="defaultMaxConnectionsPerHost" value="20"/>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: