Spring中HttpInvoker远程方法调用总结

来源:互联网 发布:网络求医 编辑:程序博客网 时间:2024/05/12 02:12

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 {@Overridepublic void startRmote() {// TODO Auto-generated method stubSystem.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();    }}


0 0
原创粉丝点击