上一篇文章我们实用JDK原生API构造了简单RMI应用,本篇将实用Spring框架来构造RMI的应用,实用Spring你会体验到简单,不需要那么多的条条框框,因为Spring给你做了很多封装。

项目构架

首先看下我的Demo构架图:

示例代码

  1. 远程接口IHello.java

    package net.oseye.DemoSpringRMI.RemotInterface;
    
    /**
    * 远程接口
    * @author oseye
    */
    public interface IHello { String sayHello(String name);
    }

    可以看到不用必须继承Remote接口和抛出异常RemoteException了,很简洁吧。

  2. 服务器端和客户端需要在pom.xml(我使用了Maven构建项目,你也可以手动添加依赖)添加Spring的context依赖

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.3.RELEASE</version>
    </dependency>

    它会自动添加相关依赖如:spring-beans等,如下图

    并引用远程接口项目进来,服务器端HelloImpl.java

    package net.oseye.DemoSpringRMI.Server;
    
    import net.oseye.DemoSpringRMI.RemotInterface.IHello;
    
    public class HelloImpl implements IHello {
    public String sayHello(String name) {
    return "Hello,"+name;
    }
    }

    可以看到也不用继承UnicastRemoteObject类了。看下spring的配置文件applicationcontext.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.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="helloService" class="net.oseye.DemoSpringRMI.Server.HelloImpl"></bean> <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="HelloService"></property>
    <property name="service" ref="helloService"/>
    <property name="serviceInterface" value="net.oseye.DemoSpringRMI.RemotInterface.IHello"/>
    <property name="registryPort" value="1199"/>
    </bean>
    </beans>

    服务器端使用了org.springframework.remoting.rmi.RmiServiceExporter类,并注入相关属性:serviceName、service、serviceInterface、registryPort等,还有更多的属性可以查看spring类库如:


    主程序App.java

    package net.oseye.DemoSpringRMI.Server;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App
    {
    @SuppressWarnings("resource")
    public static void main( String[] args )
    {
    new ClassPathXmlApplicationContext("applicationcontext.xml");
    System.out.println("服务器启动成功");
    }
    }
  3. 客户端同样需要添加spring-context依赖和引用远程接口项目(开发过程中服务器和客户都需要获得远程接口,否则无法进行,远程接口是相互协商的结果),Spring配置文件applicationcontext.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.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://10.9.146.113:1199/HelloService"/>
    <property name="serviceInterface" value="net.oseye.DemoSpringRMI.RemotInterface.IHello"/>
    </bean>
    </beans>

    主程序App.java

    package net.oseye.DemoSpringRMI.Client;
    
    import java.rmi.RemoteException;
    
    import net.oseye.DemoSpringRMI.RemotInterface.IHello;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class App {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws RemoteException {
    ApplicationContext context = new ClassPathXmlApplicationContext(
    "applicationcontext.xml");
    IHello stub = (IHello) context.getBean("helloService");
    System.out.println(stub.sayHello("kevin"));
    }
    }
  4. 运行
    至此服务器端和客户端开发完成,可以在Eclipse上多窗口运行服务器端和客户端。

    红色报警信息是因为我没有配置log4j。

问题和办法

  • 问题
    客户端和服务器在Eclipse上成功运行,但当我把它们打成可执行Jar的时候,却报告了类似这样的异常

    WARNING: Ignored XML validation warning
    org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 120; schema_reference.4: 无法读取方案文档 'http://www.springframework.org/schema/beans/spring-beans.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是。

  • 原因
    异常描述得很清楚,是无法读取“http://www.springframework.org/schema/beans/spring-beans.xsd”。
    还记得applicationcontext.xml配置文件吗?它有段是这样的

    <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.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    这得从Xml的命名空间说起,但这里不做展开,我将另起一篇来说说XML,但你需要知道这段的含义

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    它是被空格分开的两段,前一段代表命名空间,后一段代表是与其对应的验证模式。而spring首先是在META-INF/spring.schemas(打好的包里路径)搜索是否有验证模式,如果有直接取本地目录的验证文件;如果没有,就需要通过网络下载。spring.schemas内容类似

    ....
    http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd
    http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd
    http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd
    http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
    http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
    http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd
    http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd
    http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd
    http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd
    .....

    =前边代码命名空间,=号后面代表本地(Jar里)的验证模式文件路径。
    本例异常的原因就清晰了:spring首先在spring.schemas搜索命名空间“http://www.springframework.org/schema/beans/spring-beans.xsd”对应的本地验证模式路径,没有找到,于是去网上下载,但我的PC联不了外网,造成无法读取"http://www.springframework.org/schema/beans/spring-beans.xsd"。
    那么我的eclipse为什么可以运行呢?因为我的Eclipse使用了代理上网。

  • 办法
    1. 在能联公网的情况下运行服务器和客户端;
    2. 建立提供验证模式文件的内部服务,修改host;
    3. 使用本地资源:
      其实命名空间“http://www.springframework.org/schema/beans”在项目引用的“Spring-beans-4.0.3.Release.jar”里的"spring.schemas"中有,而打包后只把“Spring-context-4.0.3.Release.jar”里的"spring.schemas"打到包里了,估计是因为两个重名吧。查看“Spring-beans-4.0.3.Release.jar”里的"spring.schemas"可以看到:

      http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-4.0.xsd

      于是使用classpath把验证模式指向本地,修改如下:

      <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 classpath:org/springframework/beans/factory/xml/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

重新打包即可。关于命名空间的深入讨论请点击这里:《Java使用Schema模式对XML验证》

使用Spring构建RMI服务器和客户端的更多相关文章

  1. 教你如何构建异步服务器和客户端的 Kotlin 框架 Ktor

    Ktor 是一个使用 Kotlin 以最小的成本快速创建 Web 应用程序的框架. Ktor 是一个用于在连接系统(connected systems)中构建异步服务器和客户端的 Kotlin 框架. ...

  2. JavaEE(3) - RMI服务器和客户端

    1. 开发RMI服务器 Net Beans创建java project: (qs) (Server.java) package server; import java.rmi.*; //远程接口必须集 ...

  3. C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法

    C#调用接口注意要点   在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存c ...

  4. Spring Boot+Socket实现与html页面的长连接,客户端给服务器端发消息,服务器给客户端轮询发送消息,附案例源码

    功能介绍 客户端给所有在线用户发送消息 客户端给指定在线用户发送消息 服务器给客户端发送消息(轮询方式) 项目搭建 项目结构图 pom.xml <?xml version="1.0&q ...

  5. Java RMI 介绍和例子以及Spring对RMI支持的实际应用实例

    RMI 相关知识 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网 ...

  6. 使用XFire+Spring构建Web Service(一)——helloWorld篇

    转自:http://www.blogjava.net/amigoxie/archive/2007/09/26/148207.html原文出处:http://tech.it168.com/j/2007- ...

  7. 使用XFire+Spring构建Web Service

    XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用. 相 对于Axis来说,目 ...

  8. Spring与RMI集成实现远程访问

    使用spring对RMI的支持,可以非常容易地构建你的分布式应用.在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter ...

  9. 看完这篇包你进大厂,实战即时聊天,一文说明白:聊天服务器+聊天客户端+Web管理控制台。

    一.前言 说实话,写这个玩意儿是我上周刚刚产生的想法,本想写完后把代码挂上来赚点积分也不错.写完后发现这东西值得写一篇文章,授人予鱼不如授人以渔嘛(这句话是这么说的吧),顺便赚点应届学生MM的膜拜那就 ...

随机推荐

  1. endnote X7 加入文献

    endnote可以管理文献,并且在word中方便的添加参考文献. 1.加入文献: 2.导入以后可以创建自己的group,然后把导入的参考文献拖到group里,这样方便在插入参考文献的时候用group名 ...

  2. 分享一个基于thrift的java-rpc框架

    简单介绍 这是一个简单小巧的Java RPC框架,适用于Java平台内.为系统之间的交互提供了.高性能.低延迟的方案.适合在集群数量偏少的情况下使用(50台以下集群环境).当然.它也可以在大型集群环境 ...

  3. atom编辑器快捷键

    挑来挑去,还是决定选择atom,做为我的编程编辑器. 下面是我总结的atom快捷键 //1.atomcmd+,; 设置cmd+h; 隐藏程序cmd+alt+h; 隐藏其他程序 //2.文件cmd+n; ...

  4. jquery在Asp.net下实现ajax

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest ...

  5. 毕向东udp学习笔记2

    项目功能:  发送端读取控制台输入,然后udp发送 接收端一直接收,直到输入为886 相对于笔记1,修改了发送端代码,实现发送控制台的内容,接收端循环接收,当输入886时,停止发送 发送端: impo ...

  6. java.math.BigDecimal()的用法

    Java中简单的浮点数类型float和double是不能进行运算的,不光Java,很多语言都是这样. 我们运行下面程序你将会看到 public class TestMathDecimal { publ ...

  7. 传输控制协议TCP

    TCP主要特点: (1)面向连接 (2)只能是点到点 (3)可靠的传输协议 (4)全双工通信,各自各自的读写缓冲区,应用层吧数据交给发送缓冲区,tcp合适就发送了,接受的时候tcp写入接受缓冲区,应用 ...

  8. C# 多态理论基础

    一.概述 同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态性. 可以用不同的方式实现组件中的多态性: ● 接口多态性. ● 继承多态性. ● 通过抽象类实现的多态性. 二.实 ...

  9. C# 类型和变量

    C# 中的类型有两种:值类型 (value type) 和引用类型 (reference type).值类型的变量直接包含它们的数据,而引用类型的变量存储对它们的数据的引用,后者称为对象.对于引用类型 ...

  10. Android 获取浏览器当前分享页面的截屏

    Android 获取浏览器当前分享页面的截屏 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/BrowserScreenShotActivity 文中 ...