原文地址: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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<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.

转载于:https://www.cnblogs.com/davidwang456/p/5481497.html

Spring Remoting: Remote Method Invocation (RMI)--转相关推荐

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

    RMI(Remote Method Invocation,远程方法调用) RMI(Remote Method Invocation,远程方法调用)是用Java在JDK1.1中实现的,它大大增强了Jav ...

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

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

  3. 远程过程调用RPC RMI(Remote Method Invocation)和Web Service

    2019独角兽企业重金招聘Python工程师标准>>> 一.RPC是什么 RPC的全称是Remote Procedure call,是进程间通信方式. 他允许程序调用另一个地址空间的 ...

  4. Spring Remoting: HTTP Invoker--转

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

  5. Spring Remoting: Burlap--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...

  6. http invoker_Http Invoker的Spring Remoting支持

    http invoker Spring HTTP Invoker是Java到Java远程处理的重要解决方案. 该技术使用标准的Java序列化机制通过HTTP公开服务,并且可以看作是替代方法,而不是He ...

  7. Http Invoker的Spring Remoting支持

    Spring HTTP Invoker是Java到Java远程处理的重要解决方案. 该技术使用标准的Java序列化机制通过HTTP公开服务,并且可以被视为替代解决方案,而不是Hessian和Burla ...

  8. Spring远程支持和开发RMI服务

    Spring远程支持简化了启用远程服务的开发. 当前,Spring支持以下远程技术:远程方法调用(RMI),HTTP调用程序,Hessian,Burlap,JAX-RPC,JAX-WS和JMS. 远程 ...

  9. Spring Remoting: Hessian--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...

最新文章

  1. onAttach 显示过时的处理方法
  2. 如何使用 ClickHouse 每天玩转千亿数据,纯PPT干货
  3. Lazada2022新开店入驻流程及费用
  4. asp按钮跳转页面代码_重磅更新!全新Web编辑页面、编辑规则快速跳转、状态栏变色、富文本再次升级!...
  5. 汉诺塔算法python_经典算法:汉诺塔
  6. 揭秘!文字识别在高德地图数据生产中的演进
  7. javascript中的this使用场景
  8. .NET Task揭秘(一)
  9. LeetCode 860. 柠檬水找零(贪心)
  10. 美国街头出现怪异无人车:3个激光雷达垂直叠放
  11. linux启动关闭脚本,Linux中启动/停止/重启/状态的startup脚本
  12. android 读取 IMEI 和 MEID 的处理
  13. mysql decimal 18 2_sql语句 decimal(18,0)什么意思
  14. SRP:单一职责原则
  15. 《单基因疾病的遗传》学习笔记
  16. 马克飞象markdown用法
  17. 安装SQL2000时出现:安装程序配置服务器失败。参考服务器错误日志和 C:/WINDOWS/sqlstp.log 了解更多信息。
  18. 下载安装WindowBuilder插件教程
  19. My97date日期选择设置最大最小值
  20. 江苏理工学院计算机考研,江苏理工学院考研成绩创新高 学风建设见成效

热门文章

  1. mysql 报错注入 读文件_SQL注入-读写文件
  2. python实操题_Python100道练习题,光看不练假把式,Python实操资源
  3. java 论坛_武汉课工场JAVA培训:“真AI、超智能”人工智能大咖论坛解读
  4. java 泛型集合 json_将泛型集合转换成分页json数据
  5. redis hash key mysql_Linux取得Redis数据库中hash类型的所有feild或者所有value
  6. linux命令行ps1变量_利用Shell中变量PS1定制Linux Shell命令主提示符
  7. 城市列表简称JSON数据
  8. Ubuntu install of ROS Melodic
  9. java jni.h_java-如何使jni.h被找到?
  10. dao接口有什么好处_Java后端精选技术:我们为什么要使用AOP?