`

Spring Rmi配置

    博客分类:
  • J2EE
阅读更多
现在远程调用一般用RPC,webservice或者Rmi,而目前用的比较多的是webservice和Rmi。

webservice和rmi的最主要的区别,rmi的客户端和服务端都必须是java,webservice没有这个限制,webservice是在http协议上传递xml文本文件。与语言和平台无关,rmi是在tcp协议上传递可序列化的java对象,只能用在java虚拟机上,绑定语言。 RMI是EJB远程调用的基础,仅用RMI技术就可以实现远程调用,使用EJB是为了实现组件,事物,资源池,集群等功能.WebService是通过XML来传输数据,可用http等协议因此可在异构系统间传递,并且可以穿过防火墙,可在公网上远程调用。

以下是spring中配置Rmi的一个简单例子:

1.首先在src下建立一个接口(本人的是com.shinelife.inter):

package com.shinelife.inter;

public interface IBaseServiceRmi {
    public String SayHelloRmi(String name);
}

2.然后实现接口的方法:

package com.shinelife.services;

import com.shinelife.inter.IBaseServiceRmi;

public class BaseServiceRmi implements IBaseServiceRmi {

  public String SayHelloRmi(String name) {
          // TODO Auto-generated method stub
          return "Hello: "+name;
     }

}

3.然后就是配置remote.xml:

在src下面新建一个xml文件然后复制添加以下内容(注:头文件的对象配置很重要,配置不当会报错):

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring 远程服务对象配置-->
<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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:flex="http://www.springframework.org/schema/flex"
    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
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

   <!-- 远程对象暴露  开始 -->
   
        <bean id="BaseServiceRmiExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
         <!-- 调用Service --> 
        <property name="service">
            <ref bean="BaseServiceRmi" />
        </property>
        <!-- 客户端调用时使用的名字 -->
        <!-- value值是给用户调用 --> 
        <property name="serviceName">
            <value>BaseServiceRmi</value>
        </property>
        <!-- service 接口 -->
        <property name="serviceInterface">
            <value>com.shinelife.inter.IBaseServiceRmi
            </value>
        </property>
        <!-- 注册端口号 --> 
        <property name="registryPort">
            <value>6100</value>
        </property>
        <property name="servicePort">
            <value>7100</value>
        </property>
    </bean>
  <!-- 远程对象暴露  结束 -->
</beans>

4.本人以上的例子是自己建立的一个remote.xml文件,而一般都是直接把这些配置到spring的配置文件applicationContext.xml里,而这种在这里就不研究了。主要说说外部配置Rmi的方式。

光是添加了一个remote.xml是不够的,还需要在spring的applicationContext.xml里面注入:

<bean id="BaseServiceRmi" class="com.shinelife.services.BaseServiceRmi"></bean>

这样spring才能找到并加载这个bean。

5.由于本人的是webservice项目,所以启动服务是用的配置web.xml,也可以用main入口函数加载,对于main加载,就不讲述了,这里讲述web.xml配置,以下是web.xml的部分内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/applicationContext.xml,classpath:remote.xml
       </param-value>
    </context-param>

</web-app>

以上是服务端方面的配置,下面是客户端:

6.将com.shinelife.inter整个包拷贝到客户端下(注意服务端与客户端的包名保持一致)

7.在客户端的src下面新建一个Rmi.xml的文件,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   
    <bean id="BaseServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">       
         <!-- BaseService是调用服务端serviceName的value --> 
        <property name="serviceUrl">
            <value>rmi://127.0.0.1:6100/BaseServiceRmi</value>
        </property>
         <!-- service接口 --> 
        <property name="serviceInterface">
            <value>com.shinelife.inter.IBaseServiceRmi</value>
        </property>
    </bean>
</beans>

8.然后建立一个main函数测试Rmi服务:

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import com.shinelife.inter.IBaseServiceRmi;

public class testRmi {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:Rmi.xml"); 
        IBaseServiceRmi baseService = (IBaseServiceRmi) context.getBean("BaseServiceRmi");
        System.out.println( baseService.SayHelloRmi("Joker"));
    }
}

9.测试结果:Hello: Joker

10.总结:Rmi是一个比较高效的远程调用方式,可以将整个函数方法作为参数进行远程调用,达到通信的目的,利用Rmi也可以减少很多的工作量。当然,在其他方面本人就不做结论,本人这也是分享一下搭建Rmi的方法。以便以后学习。
分享到:
评论

相关推荐

    spring rmi 多接口配置 调用

    spring rmi 多接口服务端配置 调用多接口客户端配置

    spring与rmi 整合实例 源码详解

    spring 与rmi 的整合 1.首先编写接口 interface : 这里的接口要继承Remote, 它是一个标识接口 2.编写实现类 3.编写main方法用于启动 service 4 配置spring文件 编写客户端测试类

    spring-rmi-example:Spring rmi 示例,取自 code.google.com,因为 code.google.com 将停止使用

    spring-rmi-示例 ...这个项目是如何在 Spring 的帮助下设置 RMI 服务器和客户端的示例。 该项目包含2个子项目: Spring RMI 示例服务器,即 Web 应用程序 ...服务器 Spring 配置如下: &lt; / bean &gt; &lt; pr

    Spring 实现远程访问详解——httpinvoker

    上文我们利用Spring rmi实现了Spring的远程访问(Spring 实现远程访问详解——rmi),本文主要讲解利用HttpInvoke实现远程访问。 Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和...

    实现JMX的spring支持,拓展了RMI远程接口。

    JMX集成到spring中,并提供了一个rmi远程连接的配置文件

    Spring 实现远程访问详解——jms和activemq

    前几章我们分别利用spring rmi、httpinvoker、httpclient、webservice技术实现不同服务器间的远程访问。本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。 一. 简介 1. ...

    spring jar 包详解

    (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,...

    Hessian RPC-RMI技术 整合Structs Spring Hibernate Ibatis

    Hessian RPC-RMI技术 整合Structs Spring Hibernate Ibatis 包含Hessian配置说明、服务器Server Demo、客户端Client Demo.

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及...

    最新最全的spring开发包

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring攻略(第二版 中文高清版).part1

    1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 解决构造程序歧义 17 ...

    spring in action英文版

    第一部分 Spring基础  第1章 开始Spring之旅  1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么  1.3 开始Spring之旅  1.4 理解反向控制  1.4.1 依赖...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1 系统概述 13.2 需求分析 13.2.1 系统用例图 ...

    Spring攻略(第二版 中文高清版).part2

    1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 解决构造程序歧义 17 ...

Global site tag (gtag.js) - Google Analytics