Is it possible to use RMI bidirectional between two classes?

后端 未结 8 2004
慢半拍i
慢半拍i 2020-12-15 08:16

I want to share some information between two classes (A and B), which are running in different java programs. Instead of writing a whole communication protocol I want to use

8条回答
  •  别那么骄傲
    2020-12-15 08:53

    RMI bidirectional:

    SERVER:

    import java.io.IOException;
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class RMISERVER {
    
       public RMISERVER() throws IOException {
    
           Thread t;
            try {
                t = new Prou_run();
                t.start();
            } catch (RemoteException ex) {
                Logger.getLogger(RMISERVER.class.getName()).log(Level.SEVERE, null, ex);
            }
    
       }
    
    
       public static void main(String args[]) throws IOException {
         new RMISERVER();
       }
    }
    
    
    import java.io.File;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.registry.Registry;
    import java.rmi.server.RMIClientSocketFactory;
    import java.rmi.server.RMIServerSocketFactory;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.tree.DefaultMutableTreeNode;
    
    //extends java.rmi.server.UnicastRemoteObject
    public class Prou_run extends Thread implements Runnable{
    
                New_Object  root = null,root2=null,root3=null,root4=null,root5;
                  New_Object new_root=null;
                Object xt = null, xt2=null , xt3=null;
                Registry r1,r2;
                RMIClientSocketFactory csf,csf2;
                RMIServerSocketFactory ssf,sf2;
    
    
                new_Patryk npal;
    
     public Prou_run() throws java.rmi.RemoteException, IOException
     {
             if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
                      }
    
    //            csf = new RMIClientSocketFactory() {
    //
    //                public Socket createSocket(String host, int port) throws IOException {
    //                    return new Socket("rmi://localhost/getchil",1080);
    //                }
    //            };
    //            csf2 = new RMIClientSocketFactory() {
    //
    //                public Socket createSocket(String host, int port) throws IOException {
    //                   return new Socket("rmi://localhost/getchild",1081);
    //                }
    //            };
    //            ssf=new RMIServerSocketFactory() {
    //
    //                public ServerSocket createServerSocket(int port) throws IOException {
    //                    return new ServerSocket(1099);
    //                }
    //            };// ssf.createServerSocket(1099);
    //              sf2=new RMIServerSocketFactory() {
    //
    //                public ServerSocket createServerSocket(int port) throws IOException {
    //                   return new ServerSocket(1098);
    //                }
    //            };//sf2.createServerSocket(1098);
           try {
             r1=java.rmi.registry.LocateRegistry.createRegistry(1098);
                     r2=java.rmi.registry.LocateRegistry.createRegistry(1099);//, csf2, ssf);
                     java.rmi.registry.LocateRegistry.createRegistry(1097);
                     java.rmi.registry.LocateRegistry.createRegistry(1095);
                     java.rmi.registry.LocateRegistry.createRegistry(1096);
             System.out.println("RMI registry ready.");
          } catch (Exception e) {
             System.out.println("Exception starting RMI registry:");
             e.printStackTrace();
          }
            this.xt = null;this.xt2 = null;this.xt3 = null;
            npal = new new_Patryk();
            System.out.println("sdmmmfxxxxxxxx");
    
       }
    
    
    
    
    
    
        public void run() {
    
    //while(true){
    
          try{
    
    //             root =  new_root;
    //             xt=npal.getChild((File)new_root.getob(), (int)new_root.geti());
                 New_ObjectIMPL sl = new New_ObjectIMPL();
                 sl.i=354;
                    System.out.println("sdmmmf2");
                    //r2
                 Naming.rebind("rmi://localhost:1099/getchild",(New_Object) sl);
                    System.out.println("sdmmmf3");
    
             }catch (Exception e) {
           System.out.println("Trouble: " + e);
         }
    
            while(new_root==null){
                try{
                     //System.out.println("sdmmmf1"   +   new_root.geti());
             new_root = (New_Object) Naming.lookup("rmi://localhost:1080/getchil");
             System.out.println("sdmmmf1"   +   new_root.geti());
                }catch (Exception e) {
           System.out.println("Trouble: " + e);
         }
            }
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     *
     * @author austinchuma
     */
    public interface New_Object extends java.rmi.Remote {
    
         public int geti() throws java.rmi.RemoteException;
    
         public Object getob() throws java.rmi.RemoteException;
    
         public Object getobchild() throws java.rmi.RemoteException;
    
          public boolean getbbol() throws java.rmi.RemoteException;
    
           public byte[] getb() throws java.rmi.RemoteException;
    
    
    
    }
    
    
    
    
    
    public class New_ObjectIMPL extends java.rmi.server.UnicastRemoteObject implements New_Object
    
    {
       Object ob = null,obchild = null;
        int  i=0;
        boolean bol = false;
        byte[] b = null;
    
        public New_ObjectIMPL() throws RemoteException{
            ob = null;obchild = null;
            i=0;
            bol = false;
            b = null;
        }
    
        public int geti() throws RemoteException {
           return i;
               }
    
        public Object getob() throws RemoteException {
    
           return ob;
        }
    
        public Object getobchild() throws RemoteException {
           return obchild;
        }
    
        public boolean getbbol() throws RemoteException {
          return bol;
        }
    
        public byte[] getb() throws RemoteException {
    
            return b;
    
        }
    
    
    }
    

提交回复
热议问题