`

Spring--RMI

 
阅读更多
使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi; 
 
public interface AccountService { 
    int queryBalance(String mobileNo); 
    String shoopingPayment(String mobileNo, byte protocol); 


服务实现,示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi; 
 
import org.apache.log4j.Logger; 
 
public class MobileAccountServiceImpl implements AccountService { 
 
    private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class); 
    public int queryBalance(String mobileNo) { 
        if (mobileNo != null) 
            return 100; 
        return 0; 
    } 
 
    public String shoopingPayment(String mobileNo, byte protocol) { 
        StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append( 
                mobileNo).append("/", protocol type is /"").append(protocol) 
                .append("/"."); 
        LOG.info("Message is: " + sb.toString()); 
        return sb.toString(); 
    } 


服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
 
    <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> 
        <property name="serviceName" value="MobileAccountService" /> 
        <property name="service" ref="accountService" /> 
        <property name="serviceInterface" 
            value="org.shirdrn.spring.remote.rmi.AccountService" /> 
        <property name="registryPort" value="8080" /> 
        <property name="servicePort" value="8088" /> 
    </bean> 
 
    <bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" /> 
 
</beans> 

上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi; 
 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class RmiServer { 
 
    public static void main(String[] args) throws InterruptedException { 
        new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml"); 
         
        Object lock = new Object(); 
        synchronized (lock) { 
            lock.wait(); 
        } 
    } 


客户端调用服务
客户端配置client.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
 
    <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
        <property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" /> 
        <property name="serviceInterface" 
            value="org.shirdrn.spring.remote.rmi.AccountService" /> 
    </bean> 
 
</beans> 

配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi; 
 
import org.apache.log4j.Logger; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class RmiClient { 
 
    private static final Logger LOG = Logger.getLogger(RmiClient.class); 
     
    public static void main(String[] args) { 
        ApplicationContext ctx = new ClassPathXmlApplicationContext( 
                "org/shirdrn/spring/remote/rmi/client.xml"); 
        AccountService accountService = (AccountService) ctx 
                .getBean("mobileAccountService"); 
        String result = accountService.shoopingPayment("13800138000", (byte) 5); 
        LOG.info(result); 
    } 
 


可见,实现远程访问变得非常容易。
分享到:
评论

相关推荐

    Spring-RMI (RMI调用, HTTP调用)

    Spring-RMI (RMI调用, HTTP调用) 本人测试过了

    spring-2.5.x-3.0.x-rmi-compatibility

    spring rmi 2.5.x版本与3.0.x版本不兼容解决方案

    spring-rmi

    spring-rmi Spring RMI调用方法,本人测试过了

    Spring-RMI.rar_spring rmi

    Spring整合RMI的使用,实现不同项目间方法的远程调用。

    spring-framework-5-0-0-m3-zh_cn

    Spring Framework 是一种轻量级的解决方案,是构建你...明式事务管理,通过RMI或Web服务远程访问你的逻辑,以及用于持久存储数据的各种选项。 它提供了一个全功能的 MVC 框架,并使你能够将 AOP 透明地集成到你的软件中

    spring-framework-reference 4.3.8.RELEASE

    Framework supports declarative transaction management, remote access to your logic through RMI or web services, and various options for persisting your data. It offers a full-featured MVC framework, ...

    spring jar 包详解

    (9) spring-remoting.jar 这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    13) spring-mock.jar需spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jarspring2.0和spring2.5及以上版本的jar包区别Spring 2.5的Jar打包 在Spring 2.5中, Spring Web MVC...

    spring-rmi-example:Spring rmi 示例,取自 code.google.com,因为 code.google.com 将停止使用

    spring-rmi-示例 项目是从 code.google.com/p/springrmiexample 导出的,我这边稍作修改 这个项目是如何在 Spring 的帮助下设置 RMI 服务器和客户端的示例。 该项目包含2个子项目: Spring RMI 示例服务器,即 Web...

    SpringRMI小例子

    SpringRMI小例子代码,仅供参考。SpringRMI小例子代码,仅供参考。

    spring remoting的rmi服务2.5版本和3.0版本不兼容解决办法

    由于spring2和spring3的rmi方式调用方式不同引起的,通过查阅相关文档后发现,spring3不在需要生成skeleton和stub了,所以把这个类从spring-context中删除了,解决办法就是想办法将它再加进来

    Spring 实现远程访问详解——rmi

    1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的...

    Spring RMI小例子

    闲来无事,做了个Spring RMI的小例子,非常易懂。

    最新最全的spring开发包

    spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Spring RMI

    Spring集成rmi,实现远程服务,使用RMI集成实例

    rmi与spring整合实例

    rmi与spring整合实例

    spring-framework-4-开发手册.pdf

    Spring Framework 是一个轻...Spring Framework 支 持声明式事务管理,通过 RMI 或 Web 服务远程访问你的逻辑,并支持多种选择持久化你的 数据。它提供了一个全功能的 MVC 框架,使您能够将 AOP 透明地集成到您的软件。

    RMI-IIOP 基于SUN

    本人搜集了网上的部分资料。 并且将自己实验用的程序,说明文件,以及RMI与SPRING整和的相关资料,都集中到了资源中。 希望能对初学者有所帮助。

    spring rmi使用心得

    为了避免业务逻辑重新开发,顾使用spring rmi,把所有的bean作为rmi服务暴漏出来,在客户端只需要把项目依赖过来就ok,或者把以前的接口导入过来。 参考文档:...

Global site tag (gtag.js) - Google Analytics