A daily literacy, java's rmi,

 

 

 

On the principle and use of JAVA RMI

 

 

Local object calls

Let's take a look at the local object method call:

ObjectClass objectA = new ObjectClass(); 
String retn = objectA.Method();

 

But think about it, if objectA objects on the JVM a; and our program on the JVM b, and you want to access objectA object method on the JVM a, how to do it? For applications on the JVM b, it is not know what ObjectClass instance of an object created on the JVM a name, because this time I create an object instance may be objectA, next time I changed the program, I created another instance of an object called objectB, the other, I did not create the record ObjectClass instance object, an application on JVM b, how to know it?


RMI to solve this problem.

 

working principle

 

 

 

 


Method call from a client object via the stub (the Stub), remote reference layer (Remote Reference Layer) and the transport layer (Transport Layer) downwardly passed to the host, and then by the transport layer, through remote calls up again and a backbone network layer (Skeleton), reaches the server object. Stubs plays the role of proxy remote server object so that the object can be activated customers. Communication semantics remote reference layer processing, single or multiple management objects, the decision is to be sent to a call server or a plurality. The actual management of the transport layer connection, and track the object can accept remote method invocation. Server backbone network to complete calls to the server object actual method and get the return value. Return Value downward by remote reference layer, the transport layer server back to the client, and then upwardly through the transport layer and the return layer remote calls. Finally, the stub get a return value. 
To complete the above steps requires the following steps: 
1, to generate a remote interface 
2, remote object (server program)
3, and the stub generation backbone network (server-side program)
4, preparation of the server program 
5, the preparation of the client program 
6, registered a remote object 
7, starts a remote object  

Due to support RMI system, we only need to inherit the class write RMI applications implement the relevant interface on it. In other words, we only need to define interfaces, interface, client and server programs on it.

 

 

 

 


The stub and skeleton figure above agents are dynamically generated by the server program in the RMI system, the server program need only to extend java.rmi.server.UnicastRemoteObject class.

Then the image above RMI Service (RMI registry) is how it happened?

First secrecy:

It can be said, RMI consists of three parts, the first is a program (rmiregistry bin directory) can operate independently RMIService JDK that is provided, and the second is that is written in our own RMIServer a java project, this project outside Provide services. The third is RMIClient which we have written another java project, which used remotely RMIServer service provided.
First, RMIService must first start and begin listening for the corresponding port.
Secondly, RMIServer will achieve class services to provide their own registration to the RMIService, and specify the path (or name) for a visit RMIClient use.
Finally, RMIClient use prior knowledge (or RMIServer and a good agreement) path (or name) to RMIService go looking for this service, and the use of specific methods of this service in the local call service interface.
Popular finished speaking at a little more technique:
First, start rmiregistry service in a JVM, you can specify the service listening port, you can use the default port at startup.
Secondly, RMIServer in the local first instance of a class provide an implementation services, and then bind or rebind methods Naming, Context, Registry and other classes offered by RMI will achieve good just to instantiate the class registration to RMIService and exposed outside a name.
Finally, RMIClient through the local interface and a known name (i.e., the exposed RMIServer name) supplied reuse Naming RMI, lookup method Context, Registry, or any such implementation class to get that from RMIService. Although there is no such local implementation class of this class, but all methods in the interface, like how to adjust how to adjust it.
It is worth noting that in theory RMIService, RMIServer, RMIClient can be deployed to three different JVM, the order of execution of this time is RMIService --- RMIServer-RMIClient. Further RMIService can also be started by the RMIServer this time ordering is RMIServer-RMIService-RMIClient.

There is little practical application to provide a single RMIService server development when you can start using the Registry class RMIService in RMIServer in.

  

RMI remote invocation step
RMI interactive map:

 

 

 

RMI consists of three parts, the first is the rmiregistry (a program can run independently of the JDK, in the bin directory), the second is a server-side program, provide external remote object, the third is the client-side program, want a remote object method call.
First, let's start rmiregistry service, you can start the specified service listening port, you can use the default port (1099).
Secondly, server side to instantiate a local implementation class and services provided, and then bind or rebind method provided by RMI Naming / Context / Registry (used in the examples below Registry) just like the class is instantiated class registration to achieve good rmiregistry on the outside and expose a name.
Finally, client terminal through the local interface and a known name (i.e., exposed rmiregistry name) and then the lookup method Naming / Context / Registry like RMI classes provided from RMIService implementation class to get there. Although such a method does not implement the local class of this class, but all methods in the interface, you can call the remote object.

Stubs and backbone specific communication process:

 

 

 

Method call from a client object via the stub (Stub), remote reference layer (Remote Reference Layer) and the transport layer (Transport Layer) downwardly passed to the host, and then by the transport layer, layer through remote calls up again and a backbone network (Skeleton ), reaches the server object.
Stub plays the role of proxy remote server object so that the object can be activated customers.
Communication semantics remote reference layer processing, single or multiple management objects, the decision is to be sent to a call server or a plurality.
The actual management of the transport layer connection, and track the object can accept remote method invocation.
Backbone network to complete calls to the server object practical ways, and get a return value.
Return Value downward by remote reference layer, the transport layer server back to the client, and then upwardly through the transport layer and the return layer remote calls. Finally, the stub get a return value.

JAVA RMI simple example

This example is a client-side call subtraction method server-side remote object, the specific steps:

1. Define a remote interface (this is present in the server-side class)

java.rmi.Remote Import; 
Import java.rmi.RemoteException; 

/ * * 
 * Remote interface must be inherited. 
 * All parameters and return types must be serialized (due to the network transmission). 
 * Any remote objects must implement this interface. 
 * Only methods specified in the remote interface can be called. 
 * / 
Public  interface IRemoteMath the extends the Remote { 

      // All methods must throw RemoteException 
    public  Double the Add ( Double A, Double B) throws RemoteException;
     public  Double Subtract ( Double A, Double B) throws RemoteException; 
    
}

2, the remote interface class (present in the server computer classes)

java.rmi.RemoteException Import; 
Import of java.rmi.server.UnicastRemoteObject; 
Import remote.IRemoteMath; 

/ * * 
 * server remote interfaces. 
 * Must inherit UnicastRemoteObject, to allow the JVM to create a remote stub / proxy. 
 * / 
Public  class RemoteMath the extends the implements a UnicastRemoteObject IRemoteMath { 

    Private  int numberOfComputations; 
    
    protected RemoteMath () throws RemoteException { 
        numberOfComputations = 0 ; 
    } 
    
    @Override 
    public  Double the Add ( Double A, Double B) throws RemoteException { 
        numberOfComputations++;
        System.out.println("Number of computations performed so far = " 
                + numberOfComputations);
        return (a+b);
    }

    @Override
    public double subtract(double a, double b) throws RemoteException {
        numberOfComputations++;
        System.out.println("Number of computations performed so far = " 
                + numberOfComputations);
        return (a-b);
    }

}

3, the server side) (present on the server computer)

java.rmi.registry.LocateRegistry Import; 
Import java.rmi.registry.Registry; 
Import remote.IRemoteMath; 

/ * * 
 * RemoteMath create an instance of the class and registered in the rmiregistry. 
 * / 
Public  class RMIServer { 

    public  static  void main (String [] args) { 
        
        the try {
             // Register the remote object, the remote object provide services to clients.
            // remote object is created on the remote service, you can not know exactly the name of the object on a remote server,
             // however, register the remote object to the RMI Registry after
             // client can request through to the RMI Registry stub remote service object,
             // use proxy stub can access a remote service objects. 
            RemoteMath = IRemoteMath new newRemoteMath ();   
            LocateRegistry.createRegistry ( 1099 );     
            Registry Registry = LocateRegistry.getRegistry (); 
            registry.bind ( " Compute " , remoteMath); 
            . System OUT .println ( " the Math Server READY " );
             // If you do not want to let this object is invoked to continue using the following line
             // UnicastRemoteObject.unexportObject (remoteMath, to false); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        }         
        
    } 
    
}

4. The client (present on the client computer)

java.rmi.registry.LocateRegistry Import; 
Import java.rmi.registry.Registry; 
Import remote.IRemoteMath; 

public  class MathClient { 

    public  static  void main (String [] args) { 
        
        the try { 
             // If RMI Registry in the local machine , URL is: rmi: // localhost: 1099 / the Hello
             // otherwise, URL is: rmi: // RMIService_IP: 1099 / the Hello 
            Registry Registry = LocateRegistry.getRegistry ( " localhost " );        
             // retrieve the remote object from the Registry stub / proxy 
            IRemoteMath remoteMath = (IRemoteMath) Registry.lookup ( "Compute");
            // 调用远程对象的方法
            double addResult = remoteMath.add(5.0, 3.0);
            System.out.println("5.0 + 3.0 = " + addResult);
            double subResult = remoteMath.subtract(5.0, 3.0);
            System.out.println("5.0 - 3.0 = " + subResult);            
        }catch(Exception e) {
            e.printStackTrace();
        }
                
    }
    
}

The results are as follows:

end server

 

 

 

 

client end

 

 

href:

https://blog.csdn.net/qq_28081453/article/details/83279066#JAVA_RMI_21

https://blog.csdn.net/xinghun_4/article/details/45787549

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11596429.html