Home > Java > javaTutorial > How to write an rmi in java

How to write an rmi in java

(*-*)浩
Release: 2019-05-27 09:28:47
Original
2739 people have browsed it

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.

How to write an rmi in java

When calling such an object, its parameter is "marshalled" and sends it from the local virtual machine to a remote virtual machine (the remote virtual machine's parameter is "unmarshalled" )superior. When the method terminates, the results from the remote machine are marshalled and sent to the caller's virtual machine. If a method call results in an exception being thrown, that exception will be indicated to the caller.

When providing remote access, we first need to define what the remote end can access. In Java, defining this type of interface requires implementing the Remote interface

public interface Business extends Remote{
     public String echo(String msg) throws RemoteException;
}
Copy after login

After defining the interface, these functions We need to implement it ourselves on the server side. Therefore, we declare a class to implement the interface we provide.

public class BusinessImpl implements Business{

    @Override
    public String echo(String msg) throws RemoteException {
        if("quit".equalsIgnoreCase(msg)) {
            System.out.println("Server will be shutdown");
            System.exit(0);
        }
        System.out.println("Message from client:"+msg);
        return "Server response:"+msg;
    }
}
Copy after login

After implementing this method, one question is how to run it. Since it is remote access, there must be 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();
        UnicastRemoteObject.exportObject(business,port);
        Registry registry = LocateRegistry.createRegistry(1099);
        registry.rebind(SERVER_REGISTER_NAME, business);
    }
}
Copy after login

There are two Java classes: UnicastRemoteObject and LocateRegistry

One interface: Registry

Registry interface: Provides a remote interface for simple remote objects Used to store and obtain references to remote objects, which are obtained through arbitrary String type variable names. The bind, unbind, and rebind methods are used to change the registered names. The lookup and list methods are used to query the current Already bound object.

UnicastRemoteObject class: used to export a remote object

LocateRegistry class: an auxiliary class program used to obtain a reference to a remote calling object, mainly to build a remote object on a specific IP Remote object to accept callbacks from a specific port.

The simple server is completed, now let’s look at the client:

The client code is even simpler. We mentioned earlier that we can use the Registry’s lookup method. Get the currently bound service, so naturally, we first need to get this Registry

public class Client {

    public static void main(String[] args) throws RemoteException, NotBoundException {
        // Registry registry = LocateRegistry.getRegistry("localhost");
        Registry registry = LocateRegistry.getRegistry("localhost", 1099);
        Business business = (Business) registry.lookup(Server.SERVER_REGISTER_NAME);
        System.out.println(business.echo("Hello Server"));
    }
}
Copy after login

The above is the detailed content of How to write an rmi in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template