A brief analysis of Java RMI (similar to .net remoting) to build your own java distributed application

A brief analysis of Java RMI (similar to .net remoting) to build your own java distributed application

Both Java and .NET provide remote processing functions, but they are not exactly the same. Java remote processing is implemented through a "shared interface", while .NET can be implemented through a "shared command set". The following two methods are specifically explained.

 

Java remoting

 

Java Remote Method Invocation (RMI) provides the remote communication function of the Java programming language. This feature enables programs running on the client to call objects on the remote server, enabling Java programmers to distribute operations in a network environment.

To create a simple Java distributed remote method calling program, you can follow the following steps,

 

1. Define the remote interface:

In Java, a remote object is an instance of a class that implements a remote interface, and the remote interface declares each method to be called remotely. When we need to create a remote object, we pass an interface to hide the implementation details of the base layer, and the client can send a message through the interface handle.

The remote interface has the following characteristics:

1) The remote interface must be a public attribute. If this is not the case, unless the client and the remote interface are in the same package, when trying to load a remote object that implements the remote interface, the call will get an incorrect result.

2) The remote interface must extend the interface java.rmi.Remote.

3) Except for specific exceptions with the application itself, each method in the remote interface must declare java.rmi.RemoteException in its own throws clause. (Or the parent class of RemoteException).

4) A remote object (either directly or embedded in a local object) passed as a parameter or return value must be declared as a remote interface, not as an implementation class.

The following is the definition of the remote interface RmiSample

 

 import java.rmi.*;

 public interface RmiSample extends Remote {

     public int sum(int a,int b) throws RemoteException;

 }

 

 2. Realize the remote interface:

The remote object implementation class must extend the remote object java.rmi.UnicastRemoteObject class and implement the defined remote interface. The implementation class of the remote object contains the code that implements the remote method specified by each remote interface. This class can also contain additional methods, but clients can only use methods in the remote interface. Because the client is a handle to the interface, not its class. You must define a constructor for the remote object, even if you are only going to define a default constructor, use it to call the base class constructor. Because the base class constructor may throw java.rmi.RemoteException, so even if there is no other use, you must throw a java.rmi.RemoteException exception.

The following is the declaration of the remote object implementation class:

 

  import java.rmi.*;

  import java.rmi.server.*;

  public class RmiSampleImpl extends UnicastRemoteObject

                           implements RmiSample {

      RmiSampleImpl() throws RemoteException {

         super();

      }

      public int sum(int a,int b) throws RemoteException {

         return a + b;

      }

  }

 

Three, write the server class:

The class containing the main method can be the implementation class itself, or it can be another class entirely. Next, use RmiSampleServer to create an instance of a remote object, and use the createRegistry method of the java.rmi.registry.LocateRegistry class to start the registration service program from the specified port number. You can also start the registration service program by executing the rmiregistry command. The default value of the registration service program is The operating port is 1099. The remote object name must be bound to the reference to the remote object: Naming.rebind("//localhost:8808/SAMPLE-SERVER", Server);

The following is the declaration of the server class:

 

 import java.rmi.*;

 import java.rmi.registry.*;

 public class RmiSampleServer{

     public static void main(String args[]) {

         try {

         LocateRegistry.createRegistry(8808) ;

         SampleServerImpl Server = new SampleServerImpl();

         // Bind the object instance with the name "SAMPLE-SERVER"

         Naming.rebind("//localhost:8808/SAMPLE-SERVER" , Server);

         } catch (java.net.MalformedURLException me) {

            System.out.println("Malformed URL: " + me.toString());

         } catch (RemoteException re) {

            System.out.println("Remote exception: " + re.toString());

         }

     }

  }

 

Fourth, write a client class that uses remote services:

There are two main functions of the client class, one is to construct an instance of the registration service program stub program through the Naming.lookup method, and the other is to call a remote method on the server remote object.

The following is the declaration of the server class:

 

 import java.rmi.*;

 import java.rmi.server.*;

 public class RmiSampleClient {

     public static void main(String[] args)

     {

         try {

            String url = "//localhost:8808/SAMPLE-SERVER";

            RmiSample RmiObject = (RmiSample)Naming.lookup(url);

            System.out.println(" 1 + 2 = " + RmiObject.sum(1,2) );

         } catch (RemoteException exc) {

             System.out.println("Error in lookup: " + exc.toString());

         } catch (java.net.MalformedURLException exc) {

             System.out.println("Malformed URL: " + exc.toString());

         } catch (java.rmi.NotBoundException exc) {

             System.out.println("NotBound: " + exc.toString());

         }

      }

   }

 

Five, compile the code:

To compile Java source files, run the javac command:

 javac RmiSample.java RmiSampleImpl.java RmiSampleServer.java RmiSampleClient.java

 

6. Create root and stem for remote object implementation:

To create stub programs and skeleton files, the rmic compiler should be run with the full name of the compiled class package containing the remote object implementation.

The stub is the proxy of the remote object on the client side. It passes the RMI call to the server-side skeleton (Skeleton), which is responsible for passing the call to the actual remote method. Input is as follows:

D:/RMI>rmic -d D:/RMI RmiSampleImpl execute this command, if rmic runs successfully, there will be two new classes in the RMI directory: RmiSampleImpl_Stub.class RmiSampleImpl_Skel.class They correspond to the stub and Skeleton.

 

Seven, run the code:

Run the server program: Under Windows, enter the following command to start the RmiSampleServer program in the background:

D:/RMI>java RmiSampleServer

Run the client program:

D:/RMI>java RmiSampleClient

Client output: 1 + 2 = 3

Guess you like

Origin blog.csdn.net/fanxiaoduo1/article/details/106228920