Spring中HttpInvoker远程方法例子
2016-02-28 17:55
721 查看
本文系转载,原文地址:http://www.open-open.com/lib/view/open1408957290478.html
Spring为各种远程访问技术的集成提供了工具类。Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:
远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持。
JMS(待实现)。
目前就用到过了HttpInvoker,所以对配置进行总结下,为下一次开发奠定基础。
首先分为远程调用两部分,一个服务端,另一个是客户端。
1、定义一个接口和接口的实现类,用于客户端发请求调用的
IRemoteService.java
3、在服务端的web.xml配置上客户端要访问的请求地址,
web.xml
客户端配置:
1、在客户端定义一个与服务端一样的接口
IRemoteService.java
2、在applicationContext.xml中配置调用服务端的接口
Spring为各种远程访问技术的集成提供了工具类。Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:
远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持。
JMS(待实现)。
目前就用到过了HttpInvoker,所以对配置进行总结下,为下一次开发奠定基础。
首先分为远程调用两部分,一个服务端,另一个是客户端。
1、定义一个接口和接口的实现类,用于客户端发请求调用的
IRemoteService.java
public interface IRemoteService { public void startRmote(); }RemoteServiceImpl.java
public class RemoteServiceImpl implements IRemoteService { @Override public void startRmote() { // TODO Auto-generated method stub System.out.println("this is remote --------------------------------"); } }2、在web.xml下建立一个testRemote-servlet.xml文件。配置暴露给客户端的接口实现类信息
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="remote" class="com.frame.rmote.RemoteServiceImpl" /> <bean name="/remoteservice" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="remote" /> <property name="serviceInterface" value="com.frame.rmote.IRemoteService" /> </bean> </beans>
3、在服务端的web.xml配置上客户端要访问的请求地址,
web.xml
<servlet> <servlet-name>testRemote</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>testRemote</servlet-name> <url-pattern>/testRemoting/*</url-pattern> </servlet-mapping>服务端配置完毕
客户端配置:
1、在客户端定义一个与服务端一样的接口
IRemoteService.java
public interface IRemoteService { public void startRmote(); }
2、在applicationContext.xml中配置调用服务端的接口
<bean id="iRemoteTest" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/Frame/testRemoting/remoteservice" /> <property name="serviceInterface" value="mf.newrise.test.IRemoteService" /> </bean>测试
public class TestRemote { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IRemoteService service = (IRemoteService) applicationContext.getBean("iRemoteTest") service.startRemote(); } }
相关文章推荐
- 007_Http之常用编码
- 【UNIX网络编程(二)】基本TCP套接字编程函数
- 在有网络还是比较好解决软件上的问题
- IP地址段计算方法——计算最大广播地址,最小网络地址
- CSDN博客以后不再使用,转移到博客园了(http://www.cnblogs.com/dongc/)
- 22.网络提速(最短路)
- 22.网络提速(最短路)
- 22.网络提速(最短路)
- 22.网络提速(最短路)
- 22.网络提速(最短路)
- HTTP协议
- 网络流(最大流) POJ 1637 Sightseeing tour
- http中get与post的真正区别
- wireshark实战之:TCP协议
- CentOS6.5下使用NetHogs监控进程网络使用情况
- Nginx 的 TCP 负载均衡介绍
- https白名单设置
- 006_Http之路径
- 网络流(最大流) HDU 1565 方格取数(1) HDU 1569 方格取数(2)
- TCP 三次握手理解和过程