标签:
在博客《RMI远程方法调用技术》中使用纯Java代码演示了RMI技术的使用,本篇博客将介绍如何在Spring中使用RMI技术。
正如博客《RMI远程方法调用技术》提到的那样——RMI技术的应用通常包括在两个独立的应用程序中:RMI服务端应用程序和RMI客户端应用程序。本博客也从这两方面入手:
RMI服务端应用程序:
1、自定义远程接口
代码如下:
package com.ghj.packageofrmi;
/**
* 定义远程接口。
*
* @author 高焕杰
*/
public interface IHelloWordService{
/**
* 获取信息
*
* @author 高焕杰
*/
String getMsg();
}2、自定义远程接口实现类
代码如下:
package com.ghj.packageofrmi;
/**
* 远程接口实现类。
*
* @author 高焕杰
*/
public class HelloWordService implements IHelloWordService{
/**
* 获取信息
*
* @author 高焕杰
*/
public String getMsg(){
return "Hello World!";
}
}3、在xml文件中配置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: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">
<!-- RMI服务端 -->
<!-- RMI服务端远程接口实现类 -->
<bean id="helloWordService" class="com.ghj.packageofrmi.HelloWordService" scope="prototype"/>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 将远程接口实现类对象设置到RMI服务中 -->
<property name="service" ref="helloWordService" />
<!-- 设置RMI服务名,为RMI客户端依据此服务名获取远程接口实现类对象引用奠定基础 -->
<property name="serviceName" value="helloWord" />
<!-- 将远程接口设置为RMI服务接口 -->
<property name="serviceInterface" value="com.ghj.packageofrmi.IHelloWordService" />
<!-- 为RMI服务端远程对象注册表设置端口号-->
<property name="registryPort" value="9090" />
<!-- 其他属性可以查看org.springframework.remoting.rmi.RmiServiceExporter的类及其子类获取到-->
</bean>
</beans> 4、启动RMI服务端
代码如下:
package com.ghj.packageoftest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 启动RMI服务端
*
* @author 高焕杰
*/
public class StartRMIServer {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("conf/spring/applicationContext.xml");
System.out.println("RMI服务端启动!!!");
}
}RMI客户端应用程序:
1、自定义包含了RMI服务端自定义远程接口内的抽象方法的RMI客户端接口
代码如下:
package com.ghj.packageofrmi;
/**
* 自定义与RMI服务端自定义远程接口名称相同的接口
*
* @author 高焕杰
*/
public interface IHelloWord{
/**
* 获取信息
*
* @author 高焕杰
*/
String getMsg();
}2、在xml文件中配置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: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">
<!--RMI客户端-->
<bean id="dataExchangeProxyService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:9090/helloWord"/>
<property name="serviceInterface" value="com.ghj.packageofrmi.IHelloWord"/>
<!-- 当连接失败时是否刷新远程调用stub -->
<property name="refreshStubOnConnectFailure" value="true"/>
</bean>
</beans>3、启动RMI客户端
代码如下:
package com.ghj.packageoftest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ghj.packageofrmi.IHelloWord;
/**
* 启动RMI客户端
*
* @author 高焕杰
*/
public class RMIClient {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/spring/applicationContext.xml");
IHelloWord helloWord = (IHelloWord) ctx.getBean("dataExchangeProxyService");
System.out.println(helloWord.getMsg());
}
}【0分下载示例源码】
标签:
原文地址:http://blog.csdn.net/gaohuanjie/article/details/43083191