原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp
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-
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.
The sample program below develops a simple greeting service. The example demonstrates Spring remoting using RMI
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).
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).
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.
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:
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
>
|
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.
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).
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:
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
>
|
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.
(Alternatively you can go the folder containing the springremotingrmiserver-installer.jar and execute the jar using java -jar springremotingrmiserver-installer.jar command)
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.
(Alternatively you can go the folder containing the springremotingrmiserver-installer.jar and execute the jar using java -jar springremotingrmiclient-installer.jar command)
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.
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.