原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp

Concept Overview

Spring provides four ways to develop remote services. Remote services are services hosted on remote servers and accessed by clients over the network. For example, lets say you are developing a desktop application that needs to connect to a central server. The desktop application can be on various machines. you can use spring remoting to connect the clients on the desktop to the server. Web services developed using JAX-WS can also be developed and integrated using Spring. Here are the four remoting ways supported by spring-

  1. RMI - Remote Method Invocation - Use RMI to invoke a remote method. The java objects are serialized
  2. Hessian - Transfer binary data between the client and the server.
  3. Burlap - Transfer XML data between the client and the server. It is the XML alternative to Hessian
  4. JAX-WS - Java XML API for Web Services.

In this first tutorial we look at Spring remoting using RMI. This is how it works - Spring creates a proxy that represents the actual remote bean. The proxy is created using RmiProxyFactoryBean. The proxy can be used as a normal Spring bean and the client code does not need to know that it is actually calling a remote method. Lets look at the important classes :org.springframework.aop.framework.ProxyFactory.RmiProxyFactoryBean - This class will be used by the client to create a proxy to connect to the remote service. This is a spring FactoryBean for creating RMI proxies. The proxied service is use a spring bean using the interface specified in the ServiceInterface property. The Remote service URL can be specified by the serviceUrl property. org.springframework.remoting.rmi.RmiServiceExporter - This class will be used by the server to create a remote service. The service can be accessed by plain RMI or spring proxy class created using RmiProxyFactoryBean as explain above. This class also supports exposing non-RMI services via RMI Invoker. Any Serializable java object can be transported between the client and the server.

Sample Program Overview

The sample program below develops a simple greeting service. The example demonstrates Spring remoting using RMI

Required Libraries
  • aopalliance.jar
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar
Interaction Flow

  • Client sends a message call
  • This message call is handled by an RMI Proxy created by RmiProxyFactoryBean
  • The RMI Proxy converts the call into a remote call over JRMP (Java Remote Method Protocol)
  • The RMI Service Adapter created by RmiServiceExporter intercepts the remote call over JRMP
  • It forwards the method call to Service
RMI Server Code Package Structure

RMI Server Source Code

Create the GreetingService interface as shown below. 
Create a method named getGreeting() that takes a name as a parameter and returns the greeting message (see line 5 below).

GreetingService.java
1
2
3
4
5
6
package com.studytrails.tutorials.springremotingrmiserver;
 
public interface GreetingService {
 
String getGreeting(String name);
}

Create a class GreetingServiceImpl as shown below. 
It implements the GreetingService interface (described earlier) 
Implement the getGreeting() method by sending a greeting message (see lines 6-8 below).

GreetingServiceImpl.java
1
2
3
4
5
6
7
8
9
10
package com.studytrails.tutorials.springremotingrmiserver;
 
public class GreetingServiceImpl implements GreetingService{
 
@Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
 
}

Create the StartRmiServer class as shown below. 
It loads spring-config.xml (described later) (see line 10 below) . 
Note that loading of spring-config.xml automatically starts the RMI server.

StartRmiServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.studytrails.tutorials.springremotingrmiserver;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class StartRmiServer {
 
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-server.xml");
System.out.println("Waiting for Request from Client ...");
 
}
}

Create the spring-config-server.xml file (see below).

Declare the 'greetingService' (see lines 13-14 below).

Export the 'greetingService' using Spring's RmiServiceExporter class (see lines 16-21 below). 
Note the following properties of RmiServiceExporter class:

  • serviceName : refers the name of the server as used by the RMI Client (described later) (see line 17 below)
  • service: the service class bean which shall handle the RMI call (see line 18 below)
  • serviceInterface: The interface to be used by Spring to create proxies for RMI (see line 19 below)
  • registryPort: the default port on which the RMI service is exposed
spring-config-server.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="
 
<bean id="greetingService"
class="com.studytrails.tutorials.springremotingrmiserver.GreetingServiceImpl" />
 
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="greetingService" />
<property name="service" ref="greetingService" />
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
<property name="registryPort" value="1099" />
</bean>
 
</beans>
RMI Client Code Package Structure

RMI Client Source Code

Create the GreetingService interface as shown below. 
Copy the GreetingService interface created for RMI Server (described above) and paste it in RMI Client source code while retaining the java package structure.

Note: For reference, the source code is shown below.

GreetingService.java
1
2
3
4
5
6
package com.studytrails.tutorials.springremotingrmiserver;
 
public interface GreetingService {
 
String getGreeting(String name);
}

Create a class TestSpringRemotingRmi shown below to test Spring RMI Remoting. 
Load spring configuration file (see line 11 below) 
Get a reference to GreetingService using the bean name 'greetingService' (see line 12 below) 
Call the GreetingService.getGreting() method by passing the name 'Alpha' (see line 13 below) 
Print the greeting message (see line 14 below).

TestSpringRemotingRmi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.studytrails.tutorials.springremotingrmiclient;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.studytrails.tutorials.springremotingrmiserver.GreetingService;
 
public class TestSpringRemotingRmi {
 
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
GreetingService greetingService = (GreetingService)context.getBean("greetingService");
String greetingMessage = greetingService.getGreeting("Alpha");
System.out.println("The greeting message is : " + greetingMessage);
}
}

Create the spring-config-client.xml file (see below). 
Declare the 'greetingService' using Spring's RmiProxyFactoryBean class (see lines 13-16 below). 
Note the following properties of RmiProxyFactoryBean class:

  • serviceUrl : refers the URL of the remote service (see line 14 below). 
    Note URL part 'greetingService' corresponds to 'serviceName' property of RmiServiceExporter bean defined in spring-config-server.xml (defined earlier)
  • serviceInterface: The interface to be used by Spring to create proxies for RMI (see line 15 below)
spring-config-client.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="
 
<bean id="greetingService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/greetingService"/>
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
</bean>
 
</beans>
Running Sample Program
RMI Server Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run RMI Server Sample Program
  • Save the springremotingrmiserver-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


(Alternatively you can go the folder containing the springremotingrmiserver-installer.jar and execute the jar using java -jar springremotingrmiserver-installer.jar command)

  • You will see a wizard page as shown below

  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)

  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below. 
    This shows that the RMI Server program has run successfully on your machine

RMI Client Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run RMI Client Sample Program
  • Save the springremotingrmiserver-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


(Alternatively you can go the folder containing the springremotingrmiserver-installer.jar and execute the jar using java -jar springremotingrmiclient-installer.jar command)

  • You will see a wizard page as shown below

  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)

  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below. 
    This shows that the RMI Client program has run successfully on your machine

Browsing the Program
RMI Server Sample Code

This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotingrmiserver . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

RMI Client Sample Code

This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotingrmiclient . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

Spring Remoting: Remote Method Invocation (RMI)--转的更多相关文章

  1. Java远程方法调用(Remote Method Invocation,RMI)

    Java RMI简介: 它是Java的一个核心API和类库,允许一个Java虚拟机上运行的Java程序调用不同虚拟机上运行的对象中的方法,即使这两个虚拟机运行于物理隔离的不同主机上. Java RMI ...

  2. Spring之RMI 远程方法调用 (Remote Method Invocation)

    RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...

  3. java的RMI(Remote Method Invocation)

    RMI 相关知识RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络 ...

  4. K:java中的RMI(Remote Method Invocation)

    相关介绍:  RMI全称是Remote Method Invocation,即远程方法调用.它是一种计算机之间利用远程对象互相调用,从而实现双方通讯的一种通讯机制.使用这种机制,某一台计算机(虚拟机) ...

  5. RMI(Remote Method Invocation ) 概念恢复

    1.RMI是远程方法调用的简称,像其名称暗示的那样,它能够帮助我们查找并执行远程对象,通俗的说,远程调用就像一个class放在A机器上,然后在B机器中调用这个class的方法. 2.EMI术语 在研究 ...

  6. Spring远程调用技术<1>-RMI

    在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI)  适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...

  7. 【杂谈】对RMI(Remote Method Invoke)的认识

    前言 对RMI接触的也比较早,基本上刚学完Java基础不久就机缘巧合遇到了.当时有尝试着去了解,但是没看到比较好的教程,而且对网络编程相关知识不太了解,看了不少文章,也没弄明白.现在对网络和I/O有了 ...

  8. Lingo (Spring Remoting) : Passing client credentials to the server

    http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...

  9. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

随机推荐

  1. navigationBar设置透明

    //设置全透明方式 一.完全不用图片(iOS7之后有效)[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBar ...

  2. 销傲销售过程GSP管理系统功能概述

    1      公司介绍 西安海思威软件有限公司于2009年2月注册成立,海思威软件公司隶属于海思威集团,位于交通十分便利的西安经济技术开发区.公司致力于中国本土式销售管理的研究与管理软件产品的开发,是 ...

  3. 如何获得WPA握手包&EWSA破解WPA密码教程[zz]

    获得WPA\WPA2 握手包的方法有很多,下面介绍通过aircrack-ng工具包来载获握手数据包. 1.下载aircrack-ng工具包. 2.终端里输入 sudo airmon-ng start ...

  4. 使用Gulp和Browserify创建多个绑定文件

    Browserify是一个Javascript的绑定工具,帮助我们理顺module之间的依赖关系.Gulp用来优化workflow.两者的共同点都是使用流,但在使用流方面也有不同之处: Browser ...

  5. 修改ulimit

    ulimit 用于限制 shell 启动进程所占用的资源,支持以下各种类型的限制:所创建的内核文件的大小.进程数据块的大小.Shell 进程创建文件的大小.内存锁住的大小.常驻内存集的大小.打开文件描 ...

  6. SQL Server死锁

    SQL Server死锁 多个事务之间互相等待对方的资源,导致这些事务永久等待 注意是永久等待,而非长事务 死锁的4个条件 互斥条件(Mutual exclusion):资源不能被共享,只能由一个进程 ...

  7. Codeforces Round #384 (Div. 2) E. Vladik and cards 状压dp

    E. Vladik and cards 题目链接 http://codeforces.com/contest/743/problem/E 题面 Vladik was bored on his way ...

  8. HDU 5914 Triangle 数学找规律

    Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...

  9. ASP.NET 回调技术(CallBack)

    在asp.net中客户端与服务器端的交互默认都是整页面提交, 此时客户端将当前页面表单中的数据(包括一些自动生成的隐藏域)都提交到服务器端,服务器重新实例化一个当前页面类的实例响应这个请求,然后将整个 ...

  10. iOS杂谈-我为什么不用Interface builder

    在互联网上关于Interface Builder的争吵每天都在发生,用和不用大家都有一大堆的理由.最近看了这篇文章,很多地方和作者有共鸣,结合自己的一些经历,就有了你现在所看到的东西,你可以把它当成前 ...