• spring RMI的使用


    Spring整合RMI的原理

    客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。

    通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。

    服务端暴露远程服务

    RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。

    RMI服务端实现类

    package com.impl;
    
    import com.interfaces.IHelloWord;
    
    /**
     * Created by lunhui.wei on 2014/11/7.
     */
    public class HelloWorld implements IHelloWord{
    
        @Override
        public String helloWorld() {
            return "Hello World";
        }
    
        @Override
        public String sayHelloToSomeBody(String name) {
            return name+" say:"+" Hello world";
        }
    }

    服务端RMI接口类

    public interface IHelloWord {
        public String helloWorld();
        public String sayHelloToSomeBody(String  name);
    }

    服务端运行类

    package com;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by lunhui.wei on 2014/11/7.
     */
    public class Run {
        public static void main(String args[]){
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
            System.out.println("RMI服务伴随Spring的启动而启动了.....");
        }
    }

    服务端spring-config.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <bean id="helloWorld" class="com.impl.HelloWorld"/>
        <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
            <property name="service" ref="helloWorld"/>
            <property name="serviceName" value="hello"/>
            <property name="serviceInterface" value="com.interfaces.IHelloWord"/>
            <property name="registryPort" value="8088"/>
         </bean>
    
    </beans>

    客户端接口类直接使用服务端的接口类直接粘贴过去。

    客户端的运行类:

    import com.interfaces.IHelloWord;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by lunhui.wei on 2014/11/7.
     */
    public class Run {
        public static void main(String args[]){
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
            IHelloWord helloWord= (IHelloWord) applicationContext.getBean("helloWorld");
            System.out.println(helloWord.helloWorld());
            System.out.println(helloWord.sayHelloToSomeBody("zhangsan"));
        }
    }

    客户端Spring 的配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
            <property name="serviceUrl" value="rmi://10.61.5.14:8088/hello"/>
            <property name="serviceInterface" value="com.interfaces.IHelloWord"/>
        </bean>
    </beans>
  • 相关阅读:
    windows服务创建与管理
    html前端技术:??
    C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
    convert转化成特定日期格式
    关于android性能,内存优化(转载)
    不错的Android博客
    十步优化SQL Server中的数据访问(转载)
    数据库SQL优化大总结之 百万级数据库优化方案(转载)
    SQL Server数据库性能优化之SQL语句篇(转载)
    50种方法优化SQL Server数据库查询(转载)
  • 原文地址:https://www.cnblogs.com/weilunhui/p/4080945.html
Copyright © 2020-2023  润新知