zoukankan      html  css  js  c++  java
  • java RMI

    import java.rmi.*;
    
    public interface Hello extends Remote {
        public String getGreeting() throws RemoteException;
    }
    import java.rmi.*;
    import java.rmi.server.*;
    
    public class HelloImpl extends UnicastRemoteObject implements Hello {
        
        public HelloImpl() throws RemoteException {
            // No action needed here.
        }
    
        public String getGreeting() throws RemoteException {
            return ("Hello there!");
        }
    }
    import java.rmi.*;
    
    public class HelloServer {
        private static final String HOST = "localhost";
    
        public static void main(String[] args) throws Exception {
            // Create a reference to an
            // implementation object
            HelloImpl temp = new HelloImpl();
            // Create the string URL holding the
            // object's name
            String rmiObjectName = "rmi://" + HOST + "/Hello";
            // (Could omit host name here, since 'localhost'
            // would be assumed by default.)
            // 'Bind' the object reference to the name
            Naming.rebind(rmiObjectName, temp);
            // Display a message so that we know the process
            // has been completed
            System.out.println("Binding complete…
    ");
        }
    }
    import java.rmi.*;
    
    public class HelloClient {
        private static final String HOST = "localhost";
    
        public static void main(String[] args) {
            try {
                // Obtain a reference to the object from the
                // registry and typecast it into the appropriate
                // type
                Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
                // Use the above reference to invoke the remote
                // object's method
                System.out.println("Message received: " + greeting.getGreeting());
            } catch (ConnectException conEx) {
                System.out.println("Unable to connect to server!");
                System.exit(1);
            } catch (Exception ex) {
                ex.printStackTrace();
                System.exit(1);
            }
        }
    }

    编译运行用到的命令

    note
    
    javac Hello.java
    javac HelloImpl.java
    javac HelloServer.java
    javac HelloClient.java
    
    //This would generate both a stub fi le and a skeleton fi le. However, this stage is no
    //longer required
    rmic HelloImpl
    
    //Starting the RMI registry
    rmiregistry
    
    //start server
    java HelloServer
    
    //start client
    java HelloClient
    
    
    //result
    "Hello there!"
  • 相关阅读:
    How to join (ap invoice distributions all) AP table info with PO Table
    Order Management APIs Oe_Order_Pub
    R12 Oe_Order_Pub API
    物料搬运单
    Process Order API In Order Management(详细)
    ap_invoice_distributions_all与PO表关联问题
    分配用人单位
    oe_order_pub 更改订单行数量,提示此更改的原因未提供或无效
    Order Management APIs
    提前期
  • 原文地址:https://www.cnblogs.com/rojas/p/5390111.html
Copyright © 2011-2022 走看看