zoukankan      html  css  js  c++  java
  • SpringRMI远程方法调用【原】

    Spring为各种远程访问技术的集成提供了工具类。

    该小段引用自 http://www.open-open.com/lib/view/open1408957290478.html

    Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易。目前,Spring支持四种远程技术:

    • 远程方法调用(RMI)。通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,Spring同时支持传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的透明远程调用(支持任何Java接口)。
    • Spring的HTTP调用器。Spring提供了一种特殊的允许通过HTTP进行Java串行化的远程调用策略,支持任意Java接口(就像RMI调用器)。相对应的支持类是 HttpInvokerProxyFactoryBean和 HttpInvokerServiceExporter。
    • Hessian。通过 HessianProxyFactoryBean 和 HessianServiceExporter,可以使用Caucho提供的基于HTTP的轻量级二进制协议来透明地暴露服务。
    • Burlap。 Burlap是Caucho的另外一个子项目,可以作为Hessian基于XML的替代方案。Spring提供了诸如 BurlapProxyFactoryBean 和 BurlapServiceExporter 的支持类。
    • JAX RPC。Spring通过JAX-RPC为远程Web服务提供支持。
    • JMS(待实现)。

    远程方法调用的优点

    方便服务端与客户端之间进行对象形式的调用,而不单纯地只是调用字符串.

    而如果参数之间交互只是单纯地字符串,那么其实还不如用http的纯post通信来得爽快.

    相关下载

    本项目用的是spring3.0.6相关jar.

    项目源码样例 

    git地址: https://git.oschina.net/KingBoBo/SpringRMI.git

    项目结构

    • 红色为服务端必要文件.
    • 绿色为客户端必要文件.
    • 紫色为服务端和客户端都必要的文件.

    为了方便自测,我把服务端和客户端都整合在一个项目中.

    其实如果要深刻理解的话,您可以把红色部分用tomcat部署成服务端,绿色和紫色文件单独新建一个小项目作为纯客户端去调用服务端,这样就不会感觉本项目即是服务端又是客户端了.

    服务端相关文件

    Fruit.java

    远程方法调用时要用到的入参类,服务端和客户端可以用该参数作数据载体. 

    所以客户端也必须要有该类, 该类最好和IPerson.class一并打成jar包丢给客户端使用.

    package com.king.code.service.invoke;
    
    import java.io.Serializable;
    import java.util.Date;
    //水果
    public class Fruit implements Serializable{
        private static final long serialVersionUID = 1883838732853579826L;
        
        Integer id;//编号
        String name;//名称
        Double weight;//重量
        String color;//颜色
        Date pickDay;//采摘日期
        
        public Fruit() {
            super();
        }
        
        public Fruit(Integer id, String name, Double weight, String color, Date pickDay) {
            super();
            this.id = id;
            this.name = name;
            this.weight = weight;
            this.color = color;
            this.pickDay = pickDay;
        }
    
    
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Double getWeight() {
            return weight;
        }
    
        public void setWeight(Double weight) {
            this.weight = weight;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public Date getPickDay() {
            return pickDay;
        }
    
        public void setPickDay(Date pickDay) {
            this.pickDay = pickDay;
        }
    
        @Override
        public String toString() {
            return "Fruit [id=" + id + ", name=" + name + ", weight=" + weight + ", color=" + color + ", pickDay=" + pickDay + "]";
        }
        
        
    }

    IPerson.java

    暴露的接口类,客户端也必须要有该类, 该类最好和Fruit.class一并打成jar包丢给客户端使用.

    package com.king.code.service.invoke;
    
    /**
     * 人接口
     * @author King
     */
    public interface IPerson {
        public String eat(String fruitName);
    
        public String eat(Fruit fruit);
    }

    Person.java

    暴露的接口实现类,客户端不一定要该类,只要有它的接口父类IPerson.class即可.

    package com.king.code.service.invoke;
    /**
     * 人实现类
     * @author King
     *
     */
    public class Person implements IPerson {
        @Override    
        public String eat(String fruitName){
            System.out.println("begin------");
            System.out.println("i'm eating"+ fruitName );
            System.out.println("end------");
            return "service ha eaten " + fruitName;
        }
        
        @Override    
        public String eat(Fruit fruit){
            System.out.println("begin------");
            System.out.println("i'm eating"+fruit);
            System.out.println("end------");
            return "service has eaten "+ fruit;
        }
    }

    remote-service.xml

    相当于通常使用的spring基础配置文件,只不过配置很少就2个bean,第一个被引用,第二个被暴露.

    <?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="person" class="com.king.code.service.invoke.Person"/>
        
        <bean name="/person" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="service" ref="person" />
            <property name="serviceInterface" value="com.king.code.service.invoke.IPerson" />
        </bean> 
        
    </beans>

     web.xml

    这个不多说了

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>SpringRMI</display-name>
      <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath*:/remote-service.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

     客户端相关文件

    Fruit.java

     参考服务端Fruit.java,且整个包路径也要完全一致

    IPerson.java

     参考IPerson.java,且整个包路径也要完全一致

    remote-client-local.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="person"
                class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
                <property name="serviceUrl">
                    <value>http://localhost:8080/SpringRMI/person</value>
                </property>
                <property name="serviceInterface">
                    <value>com.king.code.service.invoke.IPerson</value>
                </property>
            </bean>
            
            
    </beans>

     Client.java Main方法入口

    package com.king.code.client.invoke;
    
    import java.util.Date;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.king.code.service.invoke.Fruit;
    import com.king.code.service.invoke.IPerson;
    /**
     * 客户端调用类
     * @author King
     *
     */
    public class Client  {
        //读取配置文件
        static ApplicationContext context = new ClassPathXmlApplicationContext("remote-client-local.xml");
        
        public static void main(String[] args) {
            eatFruit1();
            eatFruit2();
        }
        
        public static void eatFruit1()  {
            IPerson service = (IPerson) context.getBean("person");
            String  response = service.eat("苹果");
            System.out.println(response);
        }
        
        public static void eatFruit2()  {
            IPerson service = (IPerson) context.getBean("person");
            Fruit fruit = new Fruit(1,"西瓜",2.2d,"green",new Date());
            String  response = service.eat(fruit);
            System.out.println(response);
        }
        
    }

    本文原创,转载请说明出处 by 金墨痴 http://www.cnblogs.com/whatlonelytear/p/5841152.html 

  • 相关阅读:
    Fatal error: Maximum execution time of 30 seconds exceeded in
    常见变量命名规则
    Rust使用国内镜像安装依赖
    flutter使用国内镜像源
    7个每天晚上应该坚持的好习惯
    网络数据传输格式的思考
    Deepin安装 ruby 包管理工具 RVM(适用于 Debian 系列)
    C语言数据类型关键字
    win10 powershell禁止运行脚本解决
    Linux 查看系统相关信息(系统发型版本及内核版本等)
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/5841152.html
Copyright © 2011-2022 走看看