码迷,mamicode.com
首页 > 编程语言 > 详细

spring整合RMI - Java远程方法调用

时间:2014-06-03 03:33:49      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

一. 开篇语

在上一篇RMI - Java远程方法调用博文中使用的是JDK原生类进行远程方法调用, 本篇文章使用spring提供的API对RMI进行整合, 希望能给您带来帮助.


二. 核心API

1. 客户端: 客户端的核心是RmiProxyFactoryBean, 它包含两个属性serviceUrl(远程调用地址), serviceInterface(远程调用接口)


bubuko.com,布布扣


2. 服务端: RmiServiceExporter把spring管理的Bean输出成一个RMI服务, 通过把Bean包装在一个适配器类中工作, 适配器类被绑定到RMI注册表中, 并且将请求代理给服务类.


bubuko.com,布布扣


三. 环境准备

1. 运行环境:

   spring2.5, JDK6.0


2. 所需jar包: spring.jar, commons-logging.jar


3. 代码结构图:

bubuko.com,布布扣


四. 代码测试

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



spring整合RMI - Java远程方法调用,布布扣,bubuko.com

spring整合RMI - Java远程方法调用

标签:c   style   class   blog   code   java   

原文地址:http://blog.csdn.net/zdp072/article/details/27580523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!