zoukankan      html  css  js  c++  java
  • spirng整合rmi

        Java RMI 指的是远程方法调用 (Remote Method Invocation)。它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。
        在spring整合Rmi中:
    服务端使用了org.springframework.remoting.rmi.RmiServiceExporter
        RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。
    客户端使用了org.springframework.remoting.rmi.RmiProxyFactoryBean
         客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。 通过JRMP访问服务。 JRMP:Java remote method protocol,Java特有的,基于流的协议。
     
     

    服务端代码(需要使用spring的jar包和日志相关jar包):
        接口
    1. package com.rmi;
    2. /**
    3. * 接口
    4. * @author edgewalk
    5. * @date 2017年6月11日
    6. */
    7. public interface RmiServer {
    8. public boolean test();
    9. }
        实现类
    1. package com.rmi.impl;
    2. import com.rmi.RmiServer;
    3. /**
    4. * 实现类
    5. * @author edgewalk
    6. * @date 2017年6月11日
    7. */
    8. public class RmiServerImpl implements RmiServer {
    9. @Override
    10. public boolean test() {
    11. System.out.println("服务端test方法执行了.....");
    12. return true;
    13. }
    14. }
        配置文件rmi-server.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd">
    3. <beans>
    4. <!-- 定义接口实现类-->
    5. <bean id="rmiService" class="com.rmi.impl.RmiServerImpl"/>
    6. <bean id="remoteRmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
    7. <!-- service接口 -->
    8. <property name="serviceInterface" value="com.rmi.RmiServer"/>
    9. <!-- 调用Service -->
    10. <property name="service" ref="rmiService" />
    11. <!-- value值是提供给客户端调用 -->
    12. <property name="serviceName" value="remoteService"/>
    13. <!-- 注册端口 -->
    14. <property name="registryPort" value="9400"/>
    15. <!-- 服务端口 -->
    16. <property name="servicePort" value="9401"/>
    17. </bean>
    18. </beans>
        服务端启动类
    1. package com.rmi;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. /**
    5. * 服务端启动类
    6. * @author edgewalk
    7. * @date 2017年6月11日
    8. */
    9. public class MainServer {
    10. public static void main(String[] args) {
    11. System.out.println("rmi服务端启动...");
    12. ApplicationContext ac = new ClassPathXmlApplicationContext("rmi-server.xml");
    13. System.out.println("rmi服务端启动完成...");
    14. }
    15. }


    客户端代码
        接口
    1. package com.rmi;
    2. /**
    3. * 在客户端使用服务端的接口文件
    4. * @author edgewalk
    5. * @date 2017年6月11日
    6. */
    7. public interface RmiServer {
    8. public boolean test();
    9. }
    socket连接工厂(可选配置)    
    1. package com.rmi;
    2. import java.io.IOException;
    3. import java.net.Socket;
    4. import java.rmi.server.RMIClientSocketFactory;
    5. /**
    6. * 自定义的socket连接工厂
    7. *
    8. * @author edgewalk
    9. * @date 2017年6月11日
    10. */
    11. public class RMICustomClientSocketFactory implements RMIClientSocketFactory {
    12. private int timeout = 1000; // 读超时时间
    13. public void setTimeout(int timeout) {
    14. this.timeout = timeout;
    15. }
    16. public Socket createSocket(String host, int port) throws IOException {
    17. Socket socket = new Socket(host, port);
    18. /**
    19. * 调用setSoTimeout(int
    20. * timeout)可以设置超时时间,如果到了超时时间仍没有数据,read会抛出一个SocketTimeoutException,
    21. * 程序需要捕获这个异常,但是当前的socket连接仍然是有效的。
    22. */
    23. socket.setSoTimeout(timeout);
    24. return socket;
    25. }
    26. }
      配置文件(rmi-server.xml)
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd">
    3. <beans>
    4. <!-- 自定一个SCOKECT连接,可配置读超时时间 -->
    5. <bean id="rmiClientSocketFactory" class="com.rmi.RMICustomClientSocketFactory">
    6. <property name="timeout" value="1000"></property>
    7. </bean>
    8. <!-- rmi远程调用 -->
    9. <bean id="clientRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    10. <!-- rmiServer是调用服务端serviceNamevaluermiIp是服务端ip,rmiPort是服务端注册的端口 -->
    11. <property name="serviceUrl" value="rmi://127.0.0.1:9400/remoteService" />
    12. <!-- service接口 -->
    13. <property name="serviceInterface" value="com.rmi.RmiServer" />
    14. <!-- 客户端自动重连 -->
    15. <!-- lookupStubOnStartup : false表示,不在容器启动的时候创建与Server端的连接; -->
    16. <property name="lookupStubOnStartup" value="true" />
    17. <!-- refreshStubOnConnectFailure : 这个属性是表示是否连接出错时自动重连; -->
    18. <property name="refreshStubOnConnectFailure" value="true" />
    19. <!-- registryClientSocketFactory : 这个是客户端与服务端创建SOCKECT的一个工厂。 -->
    20. <property name="registryClientSocketFactory" ref="rmiClientSocketFactory" />
    21. </bean>
    22. </beans>
        测试类
    1. package com.rmi.client;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. import com.rmi.RmiServer;
    5. /**
    6. * 客户端测试调用服务端程序
    7. * @author edgewalk
    8. * @date 2017年6月11日
    9. */
    10. public class TestRmi {
    11. public static void main(String[] arg) {
    12. System.out.println("rmi客户端开始调用...");
    13. ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi-client.xml");
    14. RmiServer rmi=(RmiServer)ctx.getBean("clientRmiService");
    15. //rmi.test();
    16. System.out.println("rmi客户端调用完成...");
    17. }
    18. }


    输出结果
        服务端
    1. rmi服务端启动...
    2. log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    3. log4j:WARN Please initialize the log4j system properly.
    4. rmi服务端启动完成...
    5. 服务端test方法执行了.....
        客户端
    1. rmi客户端开始调用...
    2. log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    3. log4j:WARN Please initialize the log4j system properly.
    4. rmi客户端调用完成...
     
     
        
  • 相关阅读:
    phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接 解决办法
    MySQL意外关闭, 导致软件崩溃而无法启动的解决办法
    !function 笔记
    C++中四种类型转换方式
    SpringCloud面试题及答案
    Spring Boot面试题
    javaWeb常用面试题
    mysql行转列 问题 SUM(IF(条件,列值,0))
    mysql行列转换
    C++面试常见题
  • 原文地址:https://www.cnblogs.com/edgedance/p/6984135.html
Copyright © 2011-2022 走看看