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

  • 相关阅读:
    一周最新示例代码回顾 (4/23–4/29)
    优酷的投票
    Google API获取用户信息
    CPU性能分析
    有意思的排序算法快速排序
    http响应LastModified和ETag以及asp.net web api实现
    java/C#多态漫谈
    有意思的排序算法插入排序
    [Architecture Pattern] Repository
    50个jQuery代码段帮你成为更出色的JS开发者
  • 原文地址:https://www.cnblogs.com/warrior/p/11967131.html
Copyright © 2011-2022 走看看