一. 开篇语
在上一篇RMI - Java远程方法调用博文中使用的是JDK原生类进行远程方法调用, 本篇文章使用spring提供的API对RMI进行整合, 希望能给您带来帮助.
二. 核心API
1. 客户端: 客户端的核心是RmiProxyFactoryBean, 它包含两个属性serviceUrl(远程调用地址), serviceInterface(远程调用接口)
2. 服务端: RmiServiceExporter把spring管理的Bean输出成一个RMI服务, 通过把Bean包装在一个适配器类中工作, 适配器类被绑定到RMI注册表中, 并且将请求代理给服务类.
三. 环境准备
1. 运行环境:
spring2.5, JDK6.0
2. 所需jar包: spring.jar, commons-logging.jar
3. 代码结构图:
四. 代码测试
1. SumService: 加法运算接口
public interface SumService { public int getAdd(int a, int b); }
2. SumServiceImpl: 加法运算实现
public class SumServiceimpl implements SumService { public int getAdd(int a, int b) { return a + b; } }
3. 服务端Server
public class Server { public static void main(String[] args) { // init spring context ApplicationContext context = new ClassPathXmlApplicationContext("application_context_server.xml"); System.out.println("server start!"); } }
4. 客户端Client
public class Client { private static final Integer NUM_1 = 1; private static final Integer NUM_2 = 2; public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application_context_client.xml"); SumService sumService = (SumService) ctx.getBean("rmiProxyFactoryBean", SumService.class); System.out.println("client sum: " + NUM_1 + " + " + NUM_2 + " = " + sumService.getAdd(NUM_1, NUM_2)); } }
5. 服务端配置application_context_server.xml
<?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: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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- server --> <bean id="sumServiceImpl" class="com.service.impl.SumServiceimpl" scope="prototype" /> <!-- RMI service --> <bean id="rmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="sumServiceImpl" /> <property name="serviceInterface" value="com.service.SumService" /> <!-- expose the rmi -> rmi://127.0.0.1:8888/sumRmi --> <property name="serviceName" value="sumRmi" /> <property name="registryPort" value="8888" /> </bean> </beans>
6. 客户端配置application_context_client.xml
<?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: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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- client --> <bean id="rmiProxyFactoryBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://127.0.0.1:8888/sumRmi"/> <property name="serviceInterface" value="com.service.SumService"/> </bean> </beans>
7. 源码下载地址: http://download.csdn.net/detail/zdp072/7423185