[spring-framework]Spring中集成RMI(远程方法调用)

以前介绍过如何发布RMI服务和调用RMI服务,其实在Spring中为咱们提供了更为方便的调用方式。如今介绍下如何在Spring中集成RMI。java

首先看下实例程序目录结构:spring

Spring中发布RMI服务(ZLv_RMIServerWithSpring):ide

(1) 定义接口MessageProvider及接口中供调用的方法(MessageProvider.java):spa

package org.thera.rmi.service;

public interface MessageProvider {
	public String queryForMessage(String name);
}

(2) 实现MessageProvider接口(MessageProviderImpl.java):code

package org.thera.rmi.service;

public class MessageProviderImpl implements MessageProvider {

	@Override
	public String queryForMessage(String name) {
		return "Hello, " + name;
	}

}

作好了上述准备,下面咱们就能够经过Spring中集成RMI,方便的发布RMI服务端xml

(3) Spring配置文件做以下配置(context.xml):接口

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 注入要发布的RMI服务类 -->
	<bean id="messageService" class="org.thera.rmi.service.MessageProviderImpl"></bean>

	<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
		<!-- RMI服务名称,可自定义服务名称 -->
		<property name="serviceName" value="MessageService" />
		<!-- 导出实体 -->
		<property name="service" ref="messageService" />
		<!-- 导出接口 -->
		<property name="serviceInterface" value="org.thera.rmi.service.MessageProvider" />
		<!-- spring默认使用1099端口 -->
		<property name="registryPort" value="1199" />
	</bean>

</beans>

(4) 加载Spring容器,发布RMI服务(Main.java):ci

package org.thera.rmi.service.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml");
		System.out.println("已成功发布RMI服务类");
	}
}

    到这里,RMI的服务端已经发布成功,运行结果以下截图:
rem

Spring中客户端调用RMI服务(ZLv_RMIClientWithSpring):get

(1) 移植服务端服务接口文件MessageProvider.java;

(2) Spring配置文件作以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="messageService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://192.168.1.100:1199/MessageService" />
		<property name="serviceInterface" value="org.thera.rmi.service.MessageProvider" />
	</bean>

</beans>

(3) 加载Spring容器,调用RMI服务端(Main.java):

package org.thera.rmi.service.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.thera.rmi.service.MessageProvider;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml");
		System.out.println("加载Spring容器,并初始化RMI客户端");
		MessageProvider client = (MessageProvider)ctx.getBean("messageService");
		String temp = client.queryForMessage("LvSantorini");
		
		System.out.println("返回结果: " + temp);
	}
}

运行Main.java,结果以下图:

--------------------------------------------------------------------------------我 只 是 一 条 分 隔 线-------------------------------------------------------------------------------------------------

到这里咱们就已经介绍完Spring中集成RMI的发布和调用方式了,怎么样?相比原始方法简单了很多吧,接口文件如此简单,以前的实现java.rmi.Remote接口,每一个方法还要抛出java.rmi.RemoteException都不须要写了。

最后,有什么问题你们能够及时给我留言,共同进步!

相关文章
相关标签/搜索