Quantcast
Viewing all articles
Browse latest Browse all 4

Java RMI example – Just get starting

Java remote method invocation(Java RMI) Overview

In order to execute the binary class on remote machine, the traditional way is error-prone pattern and difficult to understand. What we do today is to use The Java Remote Method Invocation (RMI), it allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine.
RMI applications often comprise two separate programs, a server and a client. A typical server program creates some remote objects, defines remote methods to client accessible, when server is started, it waits and monitors the request from client.  A typical client program obtains a remote reference to one or more remote objects on a server and then invokes methods on them.

To develop a Java RMI application, you need follow:

  • Writing an RMI Server, The server code consists of an interface and a class.
  • Writing an RMI client, The codes looks up the remote objects and invoke calls to them.

1. Define a remote interface

The remote interface have to extend the interface java.rmi.Remote and declares a set of remote methods, each methods must throw java.rmi.RemoteException (or a superclass of RemoteException) in throws clause, in addition if needed, add throwing any application-specific exceptions.
In this example. we created interface hello and method greeting().

Hello.java
package hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String greeting() throws RemoteException;
}

The invocation to remote objects may fail due to network-related communication and server problems, it will throw a java.rmi.RemoteException.

2. Implement this interface

The server class HelloImpl need to extend class UnicastRemoteObject and implement the interface Hello.

HelloImpl.java
package hello;

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class HelloImpl extends UnicastRemoteObject implements Hello {
    public HelloImpl() throws RemoteException {
    }

    public String greeting() throws RemoteException {
        return "greeting";
    }

}

3. Generate the required stub and skeleton using RMIC.

If you use JDK 1.5 or later, you can bypass this step, because JDK 1.5 or later has internally implemented this convention. But If use JDK1.4 or prior to 1.4, follow the following command to manually generate the stub and skeleton using RMIC:

rmic [ options ] package-qualified-class-name(s)

example: % rmic -d C:\java\classes foo.MyClass

4. Create a server to start Java RMI registry, server

The server has a start entrance main method. It create Registry in JVM in default port 1099, and then rebind the instance of the remote object implementation to exports it.

RMIServer.java
package hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;

public class RMIServer {

    public static void main(String[] args) throws RemoteException, MalformedURLException {
        LocateRegistry.createRegistry(1099);
        Hello hello = new HelloImpl();
        Naming.rebind("server.Hello", hello);
        System.out.println("server.RMI Server is ready.");
    }
}

Once the Java RMI server is started, the console will output “server.RMI Server is ready.”.

5. Create a client program to call remote RMI server method

The Java RMI client is a relatively simple program, it begins by installing a security manager. This step is necessary because the process of receiving the server remote object’s stub could require downloading class definitions from the server. then constructs a name to use to look up a server.Hello remote object, after getting the reference object, directly call the remote methods.

RMIClient.java
package hello;

import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.MalformedURLException; 

public class RMIClient {
    public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry("localhost");
        Hello hello = (Hello) registry.lookup("server.Hello");
        System.out.println(hello.greeting());
    }
}

Finally, if the console of server outputs "greeting", congratulates, you have successfully completed this tutorial - get starting for java RMI.


Viewing all articles
Browse latest Browse all 4

Trending Articles