Spring中HttpInvoker遠程方法調用總結
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(待實現)。 </ul>
目前就用到過了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 --------------------------------"); } }
<?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
客戶端配置:
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>
測試:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!