spring rmi实际上是扩展了下java rmi的实现,可以使用bean的xml配置方式使用rmi。
RMI (Remote Method Invocation)是从JDK 1.1开始就出现的API功能,它让客户端在使用远端服务所提供的服务时,就如何使用本地服务一样,然而RMI在使用时必须一连串繁复的手续,像是服务介面在定义时必须继承java.rmi.Remote介面、服务Server在实作时必须继承java.rmi.UnicastRemoteObject类别、必须使用rmic指令产生stub与skeleton等,设定上手续繁杂。 您可以在Spring中透过org.springframework.remoting.rmi.RmiServiceExporter来简化使用RMI的手续,来实际看看例子,了解Spring在RMI上的使用与简化,首先来看一下RMI伺服端的撰写,首先定义一个服务接口:
package org.spring;
public interface RmiService {
public String doWork();
public int add(int a, int b);
}
服务物件的接口不用继承java.rmi.Remote介面,而在实现R miService 时也不用继承java.rmi.UnicastRemoteObject类别,
package org.spring;
public class RmiServiceImpl implements RmiService{
@Override
public String doWork() {
return "this message return from server";
}
@Override
public int add(int a, int b) {
return a+b;
}
}
这只是个简单的示例
接下来您只要在Bean定义档中定义,让Spring管理、生成Bean实例,如此即可注册、启动RMI服务,
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="rmiService"
class="org.spring.RmiServiceImpl"/>
<bean id="serviceExporter"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<ref bean="rmiService"/>
</property>
<property name="serviceName">
<value>rmiService</value>
</property>
<property name="serviceInterface">
<value>org.spring.RmiService</value>
</property>
</bean>
</beans>
只要告诉org.springframework.remoting.rmi.RmiServiceExporter服务物件、名称与要代理的介面,之后Spring读取完定义档并生成Bean实例后,RMI服务就会启动,来撰写一个简单的RMIServer类别,以启动RMI服务:
package org.spring;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIServer {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("config/rmi-server.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (reader.readLine().equals("exit")) {
System.exit(0);
}
}
}
}
接下来,在客户端,只要依赖接口对应的jar包就可以了,然后再spring的配置文件中配置好需要访问的服务的地址和对应的接口名称
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="rmiServiceProxy"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/rmiService</value>
</property>
<property name="serviceInterface">
<value>org.spring.RmiService</value>
</property>
</bean>
</beans>
注意到"serviceUrl"属性的设定,它是以"rmi://"开头,接着指定伺服器位址与服务名称,来撰写个简单的客户端程式以使用RMI伺服器上的服务
package org.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIClient {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"config/rmi-client.xml");
RmiService service =
(RmiService) context.getBean("rmiServiceProxy");
String result1 = service.doWork();
System.out.println(result1);
int result2 = service.add(1, 2);
System.out.println(result2);
}
}
然后我们执行RMIServer.java类,在spring web应用中只要配置文件加载到spring的加载路径即可应用。
然后再执行RMIClient.java类,执行结果如下:
这样使用spring就能很方便的简化java 的rmi调用,并且由spring管理,在分布式的应用中就能做到客户端只依赖接口,不依赖实现。