标签:c style class blog code java
一. 开篇语
在上一篇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); }
public class SumServiceimpl implements SumService { public int getAdd(int a, int b) { return a + b; } }
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!"); } }
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)); } }
<?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>
<?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>
spring整合RMI - Java远程方法调用,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://blog.csdn.net/zdp072/article/details/27580523