zoukankan      html  css  js  c++  java
  • RMI

    Java RMI 指的是远程方法调用 (Remote Method Invocation)。它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。

    示例

    interface

    package me.warriorg.rmi.remote;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    /**
     * @author warrior
     */
    public interface Hello extends Remote {
        /***
         *
         * @return
         */
        String sayHello() throws RemoteException;
    }
    
    

    server

    package me.warriorg.rmi.server;
    
    import me.warriorg.rmi.remote.Hello;
    
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
    
    /**
     * @author warrior
     */
    public class Server implements Hello {
    
        public static void main(String args[]) {
    
            try {
                Server obj = new Server();
                Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    
                // Bind the remote object's stub in the registry
                Registry registry = LocateRegistry.createRegistry(8888);
                registry.bind("Hello", stub);
    
                System.err.println("Server ready");
            } catch (Exception e) {
                System.err.println("Server exception: " + e.toString());
                e.printStackTrace();
            }
        }
    
        @Override
        public String sayHello() throws RemoteException {
            return "Hello, world!";
        }
    }
    

    client

    package me.warriorg.rmi.client;
    
    import me.warriorg.rmi.remote.Hello;
    
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    
    /**
     * @author warrior
     */
    public class Client {
        public static void main(String[] args) {
    
            String host = "localhost";
            int port = 8888;
            try {
                Registry registry = LocateRegistry.getRegistry(host, port);
                Hello stub = (Hello) registry.lookup("Hello");
                String response = stub.sayHello();
                System.out.println("response: " + response);
            } catch (Exception e) {
                System.err.println("Client exception: " + e.toString());
                e.printStackTrace();
            }
        }
    }
    

    参考

    https://docs.oracle.com/javase/tutorial/rmi/overview.html

  • 相关阅读:
    变量
    注释 & 缩进
    【oracle】生成AWR报告
    【Linux】awk指令
    rhel7.0替换centos yum源
    如何在乌班图上配置java开发环境
    如何在乌版图系统添加拼音输入法!
    如何让虚拟机中乌版图系统变大?
    如何重置虚拟机的乌版图系统的密码?
    虚拟机三种网络模式配置
  • 原文地址:https://www.cnblogs.com/warrior/p/11967131.html
Copyright © 2011-2022 走看看