ITPub博客

首页 > 应用开发 > IT综合 > Spring中集成RMI(远程方法调用)

Spring中集成RMI(远程方法调用)

IT综合 作者:xz43 时间:2016-04-06 17:53:22 0 删除 编辑

Spring有多种调用远程的方式,今天学习了一下远程方法调用(RMI)。

RMI需要服务端和客户端


现在介绍下如何在Spring中集成RMI。

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

Spring中发布RMI服务(ZLv_RMIServerWithSpring):

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

?
1
2
3
4
5
package org.thera.rmi.service;
 
public interface MessageProvider {
    public String queryForMessage(String name);
}

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

?
1
2
3
4
5
6
7
8
9
10
package org.thera.rmi.service;
 
public class MessageProviderImpl implements MessageProvider {
 
    @Override
    public String queryForMessage(String name) {
        return "Hello, " + name;
    }
 
}

做好了上述准备,下面我们就可以通过Spring中集成RMI,方便的发布RMI服务端

(3) Spring配置文件作如下配置(context.xml):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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):

?
1
2
3
4
5
6
7
8
9
10
11
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的服务端已经发布成功,运行结果如下截图:

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

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

(2) Spring配置文件做如下配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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都不需要写了。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9399028/viewspace-2076328/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
鱼儿的学习空间

注册时间:2010-11-16

  • 博文量
    433
  • 访问量
    1863699