RMI refers to Remote Method Invocation. It is a mechanism that allows an object on a Java virtual machine to call a method on an object in another Java virtual machine. Any object that can be called with this method must implement the remote interface.
When such an object is called, its parameter is "marshalled" and is sent from the local virtual machine to the remote virtual machine (the parameter of the remote virtual machine is "unmarshalled"). When this method terminates, the results from the remote machine are marshalled and sent to the caller's virtual machine. If a method call causes an exception to be thrown, the exception will be indicated to the caller.
When providing remote access, we first need to define what the remote can access. In Java, defining this type of interface requires the implementation of the Remote interface.
public interface Business extends Remote{ public String echo(String msg) throws RemoteException; }
After defining the interface, these functions need to be implemented by us on the Server side. Therefore, we provide the interface by declaring a class implementation.
public class BusinessImpl implements Business{ @Override public String echo(String msg) throws RemoteException { if("quit".equalsIgnoreCase(msg)) { ("Server will be shutdown"); (0); } ("Message from client:"+msg); return "Server response:"+msg; } }
After implementing this method, there is a question: how to run it? Since it is a remote access, it must have a port number and an instance, so we also need to register our code.
public class Server { public static final String SERVER_REGISTER_NAME = "BusineeDemo"; public static void main(String[] args) throws RemoteException { int port = 2016; Business business = new BusinessImpl(); (business,port); Registry registry = (1099); (SERVER_REGISTER_NAME, business); } }
Here are two Java classes: UnicastRemoteObject and LocateRegistry
One interface: Registry
Registry interface: Provide a remote interface to a simple remote object to provide storage and obtain references to remote objects, and these are obtained by arbitrary String type variable names. The bind, unbind, rebind methods are used to change the registered names, and the lookup and list methods are used to query the currently bound object.
UnicastRemoteObject class: used to export a remote object
LocateRegistry class: is a helper class program used to obtain references to remote call objects. It mainly builds a remote object on a specific IP to accept callbacks from a specific port.
The simple server has been completed, now let’s look at the client:
The client code is simpler. We mentioned earlier that we can obtain the currently bound service through the Registry lookup method, so it is natural that we must first obtain this Registry.
public class Client { public static void main(String[] args) throws RemoteException, NotBoundException { // Registry registry = ("localhost"); Registry registry = ("localhost", 1099); Business business = (Business) (Server.SERVER_REGISTER_NAME); (("Hello Server")); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.