RMI Implementation using JAVA.

This lecture will teach us how to create RMI server and client practical.

Required 4 Java file to create server and client.

1. Interface

2. Function to host

3. Server

4. Client

STEPS

  1. First of all create 2 folder one for servers and other four client.
  1. The process of creating common interface between server and client required to implement an interface. This interface will have list of a function to be host. Again this interface is inherited from “Remote” class.

Copy the following code, save as “IHello.java”.

import java.rmi.*;

public interface IHello extends Remote{

public String say() throws RemoteException;

}

Explanation

Above example create an interface which extended to the remote class. And here we have defined one function name “say()” which will return string and which may throw an exception which known as “remote exception”. It is compulsory to write because you’re working over a network. There are many constraints which afact make network communication. Now, your code and generate class file. Copy this class file to client folder.

  1. Now we are going to implement our function in class “Hello”

import java.rmi.*;

import java.rmi.server.*;

public class Hello extends UnicastRemoteObject implements IHello{

String str= “”;

public Hello(String str) throws RemoteException

{                              this.str= str;

}

public String say() throws RemoteException

{              return str;

}

}

Explanation

here we are creating one file with name Hello.java , which contained hello class. Again these class is extended to the “unicast remote object” (this is define in java.rmi.server Package ) and implemented our created IHello interface. Here I’m creating one constructer to receive a string and say() function defined in IHello interface. Which will return the string. Again you will notice that all the function defined above may throw an exception. So we have return keyword “throws remote exception”.

Compile your Hello file and generate .Class file. Once the class file is generated is time to generate a our STUB.

JDK comes with another compiler for RMI known as RMIC (RMI compiler). This compiler help me to generate our stub.

RMIC Hello

follow the command given above you will have one of the class file with name “Hello_Stub.class” . This is the our stub file. Copy this stub file to the client folder.

  1. Now it’s time to create server. Which will host our function, open port for us as well as provide a request response services.

 

import java.rmi.*;

import java.rmi.registry.*;

 

public class myserver{

public static void main(String arg[])

{

try{

Registry r = java.rmi.registry.LocateRegistry.createRegistry(1900);

r.rebind(“Hello”,new Hello(“This is Server”));

System.out.println(“Server Exit”);

}catch(Exception e)

{

System.out.println(e.toString());

}

 

}

}

Explanation

This piece of code is sever. Here I need to create one registry. Registry is the one of the service which provide the availability of any function. For that we will import package “Java.RMI.registry”. Then we will create object “r” with Registry using “LockateRegistry.createRegistry(PORT NO)”. We assigned port number and we will register over server to registry.

Now it is time, to bind our function with name will perform that by “rebind” method as even above example. Your server is ready. Compile the server before running over server we need to start registry.

  1. Before running the server, we will start registry. This RMI registry is again provided by JDK. Begin start this registry by giving command.

 

START RMIREGISTRY

It will open one black DOS command prompt. Don’t worry about it. Let it keep in running state.

  1. In this step, we will implement our client. And this whole lecture our client is responsible for consuming the services/function hosted by our server that is function “say()”. This function is return string value from server to client. We will catch that string and display to our console. Let’s see how we can do that.

import java.rmi.*;

 

 

public class myclient{

 

public static void main(String args[])

{

try{

IHello _hello = (IHello)Naming.lookup(“//localhost:1900/Hello”);

String str = _hello.say();

System.out.println(str);

}catch(Exception e)

{

System.out.println(e.toString());

}

 

}

}

Explanation

Here we have created _hello object of IHello type and we are looking for services running at the server. By providing IP address or hostname with port number we can access the function reside in server process. Which is the core component of RMI philosophy. To access the function using the object _hello we need to use naming.lookup Method. Now we can access “Say()” method from our _hollo object using _hello.say().

Now it’s time to compiler our client for checking any error.

Now As the final stage will be run over server at one console and remain it in running state. Open new DOS prompt and run your client. Which will have an answer coming from server.

 

————————– This document has been written with speech to text recognition software developed at our drjslab.org, So it may have a some grammatical mistake. If you find such things feel free to inform to the administrator as soon as possible thanks for Corporation. ————————————————————————————————–

Share Post