`
aoaoao
  • 浏览: 23322 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

【转贴】Spring RMI

阅读更多
Remote Method Invocation (RMI) Model:

RMI was first introduced in JDK 1.1.  But developing and accessing  RMI services involves various steps and also have lookups which makes the code hard to test. Spring simplifies the RMI by providing a 'proxy factory bean' that enables us to wire the RMI services into spring application as if they were local beans. Spring also provides a remote exporter that converts our 'spring managed beans' into RMI services.

Spring's 'RmiProxyFactoryBean' is a factory bean that creates a proxy to RMI service. It is declared spring configuration file under the <bean> tag as follows,

<bean    id="service1"   

         class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

   <property name="serviceUrl">

      <value>rmi://${hostname}/service1</value>  

   </property>

   <property name="serviceInterface">

      <value>service1</value>

   </property>

</bean>

The url of the RMI service is set through the 'serviceUrl' property. The 'serviceInterface' property specifies the interface that the service implements and only through that the client invokes methods on the service.

For using the service the implementation  code is wired to the RMI using the following code,

<bean id="serviceimpl" class="serviceimpl">

   <property name="service1">

       <ref bean="service1"/>

   </property>

</bean>

-------------------------------------------

First set the path and classpath as before. Next edit the RMI service.

//f:\springdemo\rmserver.java

import java.rmi.*;

public  interface  rmserver  extends Remote

{

   String   getresult(String   s)  throws RemoteException;

}

----------------------------------------------

//f:\springdemo\rmserverimpl.java

import java.rmi.*;

import java.rmi.server.*;

public class rmserverimpl extends UnicastRemoteObject

                             implements rmserver

{

   public static void main(String args[])

   {

      try

      {

         rmserverimpl   ob = new rmserverimpl();

         Naming.rebind("rmserver",ob);

         System.out.println("ready");

      }

      catch(Exception e1)

         {System.out.println(""+e1);}

   }

   public rmserverimpl() throws RemoteException

   {

   System.out.println("constructor ok");

   }

   public String getresult(String  a)  throws RemoteException

   {

       return "Hai..."+a;

   }

}

----------------------------------------------

//f:\springdemo\rmserver.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 id="rmserver"

        class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

   <property name="serviceUrl">

       <value>rmi://localhost/rmserver</value>  

   </property>

   <property name="serviceInterface">

       <value>rmserver</value>

   </property>

  </bean>

  <bean     id="rmserverimpl"    class="rmserverimpl">

      <property name="rmserver">

          <ref bean="rmserver"/>

      </property>

  </bean>

</beans>

---------------------------------------------

//f:\springdemo\rmspring.java

import java.rmi.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

public class  rmspring

{

   public static void main(String args[])

   {

      try

      {

         System.out.println("Wait..");

         Resource     res = new ClassPathResource("rmi.xml");

         BeanFactory   factory = new XmlBeanFactory(res);

         rmserver bean1 = (rmserver) factory.getBean("rmserver");

         String r=bean1.getresult(args[0]);

         System.out.println(r);

      }

      catch(Exception e1)

          {System.out.println(""+e1);}

   }

}

---------------------------------------

To run:

f:\springdemo>javac rmserver.java

f:\springdemo>javac rmserverimpl.java

f:\springdemo>rmic rmserverimpl (To create stub and skeleton)

f:\springdemo>javac rmspring.java

f:\springdemo>start rmiregistry (a blank window will appear)

f:\springdemo>java rmserverimpl

Open another Window and run the client code by giving the argument

f:\springdemo>java rmspring "sam"

We will get the output as:

Wait.. 
......
Hai... sam

Here we have removed the 'lookup' code in the client side.

-----------------------------------------------------------------

Spring also supports the server side of RMI. Here the service itself is written with spring and it is exposed as an RMI service. Here the bean is written as a simple JavaBean. Also we need not generate the stub and skeleton using 'rmic' command and manually add it to RMI registry. Instead of these traditional procedure 'RmiServiceExporter' is used to export any Spring managed bean as an RMI service. It wrapps the bean in an adapter class. The adapter class is then bound to RMI registry and the proxies request the service.

<bean     class="org.springframework.remoting.rmi.RmiServiceExporter">

   <property name="service1">

       <ref bean="service1"/>

   </property>

   <property name="serviceName">

        <value>service1</value>

   </property>

   <property name="serviceInterface">

        <value>service1</value>

   </property>

</bean>

The 'serviceName property' indicates the name of service and 'serviceInterface' specifies the interface implemented by the service. There is no need of 'serviceUrl' here

First set the path and classpath as before. Next edit the service.

//f:\springdemo\rmservice.java

public  interface  rmservice

{

    String     getresult(String    s);

}
------------------------------------------

//f:\springdemo\rmserviceimpl.java

public class rmserviceimpl  implements    rmservice

{

   public static void main(String args[])

   {

      System.out.println("ready");

   }

   public rmserviceimpl()

   {

      System.out.println("constructor ok");

   }

   public String getresult(String  a)

   {

      return "Hai"+a;

   }

}

------------------------------------------

//f:\springdemo\rmservice.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    class="org.springframework.remoting.rmi.RmiServiceExporter">

   <property name="service">

       <value>rmservice</value>  

   </property>

   <property name="serviceName">

       <value>service1</value>  

   </property>

   <property name="serviceInterface">

       <value>rmservice</value>

   </property>

   </bean>

   <bean id="rmservice" class="rmserviceimpl">

   </bean>

</beans>

------------------------------------------

//f:\springdemo\rmserviceclient.java

import java.io.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

class   rmserviceclient

{

   public static void main(String args[])

   {

      try

      {

         System.out.println("Wait..");

         Resource  res = new ClassPathResource("rmservice.xml");

         BeanFactory  factory = new  XmlBeanFactory(res);

         System.out.println("factory created");

         rmservice bean1 = (rmservice)factory.getBean("rmservice");

         String s = bean1.getresult(args[0]);

         System.out.println(s);

      }

        catch(Exception e1)

               {System.out.println(""+e1);}

   }

}

---------------------------------------

To run:

f:\springdemo>javac rmservice.java

f:\springdemo>javac rmserviceimpl.java

f:\springdemo>javac rmserviceclient.java

f:\springdemo>java rmsserviceclient

We will get Output as:

Wait..

Aug 12, 2002 10:55:07 PM  

     org.springframework.beans.factory.

     xml.XmlBeanDefinitionReader

     loadBeanDefinitions

INFO: Loading XML bean definitions from

      class path resource[rmservice.xml]

Aug 12, 2002 10:55:07 PM

     org.springframework.beans.factory.

     support.AbstractBeanFactory getBean

INFO: Creating shared instance of

      singleton bean 'rmservice'

constructor   ok

Hai...sam  

Here the service interface doesn't extend the 'java.rmi.Remote' method and 'RemoteException' is not thrown by the methods. There is no binding in the implementation code. Also we can direcly run the client. No run to run 'rmserverimpl' first. Also there is no need to run the RMI registry.
分享到:
评论

相关推荐

    Spring RMI小例子

    Spring RMI(Remote Method Invocation)是Java平台上的远程方法调用技术,结合Spring框架,能够帮助开发者轻松地创建分布式应用程序。在这个"Spring RMI小例子"中,我们将深入理解Spring如何简化RMI的使用,以及...

    Spring RMI

    **Spring RMI 深度解析** Spring框架作为Java企业级应用开发的首选,提供了丰富的功能,包括对远程方法调用(Remote Method Invocation,RMI)的支持。RMI是Java平台上的一个核心特性,允许在分布式环境中进行对象...

    SpringRMI小例子

    Spring Remote Method Invocation(RMI)是Java平台上的一个远程对象调用框架,它允许一个Java对象在一台机器上执行,并且被另一台机器上的客户端调用。在这个"SpringRMI小例子"中,我们将深入探讨如何利用Spring...

    spring rmi 改造

    在IT行业中,Spring框架是Java开发中的一个基石,它提供了丰富的功能来简化应用程序的构建,包括远程过程调用(Remote Method Invocation,RMI)服务。本文将深入探讨"spring rmi 改造"这一主题,主要关注如何在原有...

    spring rmi应用

    本文将深入探讨“Spring RMI(Remote Method Invocation)应用”,这是Spring框架中用于实现远程方法调用的功能,它使得分布式系统开发变得更加简单。 首先,RMI是Java平台上的一个核心特性,允许在不同JVM之间透明...

    spring rmi使用心得

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

    spring rmi 小例子

    Spring Remote Method Invocation (RMI) 是Java平台上的一个远程对象调用框架,它允许你在分布式环境中调用对象的方法。在本示例中,我们将探讨如何使用Spring RMI创建一个小的应用程序,这通常涉及到服务器端(服务...

    spring RMI简单例子

    Spring RMI(Remote Method Invocation)是Java平台上的远程方法调用技术,结合Spring框架的特性,使得在分布式系统中调用远程服务变得更加便捷。在这个简单的例子中,我们将深入理解Spring RMI的工作原理以及如何...

    spring RMI 服务(自动把service发布为RMI服务)

    Spring RMI服务是一种在Java平台上实现远程方法调用(Remote Method Invocation, RMI)的技术,它允许分布式系统中的不同组件通过网络进行通信。在Spring框架的支持下,我们可以更方便地将服务发布为RMI服务,使得...

    Spring Rmi使用文档

    ### Spring RMI 使用详解 #### 一、Spring RMI 概述 Spring RMI 是 Spring 框架中用于支持远程方法调用(Remote Method Invocation)的功能模块。通过 Spring RMI, 开发者能够更加简便地搭建和管理远程服务。传统上...

    spring rmi 源码

    Spring RMI(Remote Method Invocation)是Spring框架对Java RMI技术的一种封装,使得在Spring环境中使用RMI变得更加简便。RMI是一种Java平台上的远程对象调用机制,它允许一个Java对象在不同的Java虚拟机之间调用另...

    spring RMI 实用分享

    Spring RMI(Remote Method Invocation)是Java平台上的远程方法调用技术,允许程序在不同的Java虚拟机(JVM)之间进行通信,实现分布式系统。在本文中,我们将深入探讨Spring框架如何集成RMI,以及如何创建和使用...

    Spring-RMI.rar_spring rmi

    1.2 Spring的RMI支持:Spring通过`org.springframework.remoting.rmi.RmiServiceExporter`和`RmiProxyFactoryBean`简化了RMI的使用。`RmiServiceExporter`用于发布服务,而`RmiProxyFactoryBean`则用于创建RMI服务的...

    spring rmi 集成

    **Spring RMI 集成详解** 在Java开发中,远程方法调用(Remote Method Invocation,RMI)是一种用于在不同Java虚拟机之间进行对象交互的技术。Spring框架提供了对RMI的支持,使得在Spring应用中集成RMI变得更加简单...

    spring和RMI分布式整合源码

    在IT行业中,Spring框架与RMI(Remote Method Invocation,远程方法调用)的结合是构建分布式系统的一种常见方式。这个“spring和RMI分布式整合源码”可能包含了一个实际项目中如何将这两种技术融合的实例。现在,...

    spring rmi 简单应用

    Spring RMI(Remote Method Invocation)简单应用主要涉及的是在Java中使用Spring框架来实现远程方法调用的技术。RMI是Java提供的一种分布式计算能力,它允许一个Java对象调用网络另一端的Java对象的方法,实现了...

    springRMI接口实现

    Spring Remote Method Invocation (RMI) 是Spring框架提供的一种远程服务调用机制,它允许你在分布式环境中调用对象的方法,就像是在本地执行一样。这个技术基于Java的RMI系统,但通过Spring的抽象层,简化了配置和...

    java项目使用spring rmi所涉及到的包

    在Java项目中,Spring RMI(Remote Method Invocation)是一种整合远程方法调用功能与Spring框架的方式,它使得在分布式环境中管理对象和服务变得更加便捷。本文将深入探讨Spring RMI涉及的包,以及如何在项目中使用...

    spring RMI 远程接口调用

    Spring RMI(Remote Method Invocation)远程接口调用是Spring框架提供的一个特性,它允许你在分布式环境中调用对象的方法,使得应用程序能够跨越网络边界操作远程对象。这个技术在大型企业级应用中尤其有用,因为它...

Global site tag (gtag.js) - Google Analytics