Monday, September 7, 2009

Java RMI Interview Questions

Does RMI-IIOP support code downloading for Java objects sent by value across an IIOP connection in the same way as RMI does across a JRMP connection?

Yes. The JDK 1.2 support the dynamic class loading.

Can RMI and Corba based applications interact ?

My Software environment for this example:

1. Eclipse 3.2

2. JDK 1.5

Idea here is to show way to call a remote method from a different JVM

from a command line based application.

So we have two command line based program, one program locates an already

running RMIRegistry with a port number 1099, and binds the remote object

(an object from a class that extends java.rmi.server.UnicastRemoteObject

and implements an interface that extends java.rmi.Remote).

And the other program locates an already running RMIRegistry and lookup

the already bound remote object.

So we have to define an interface that extends java.rmi.Remote

and a class that extends UnicastRemoteObject and implements this interface.

ExampleRemote.java

//This code is provided "AS IS"

package example;

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface ExampleRemote extends Remote {

public String test() throws RemoteException;

}

ExampleRemoteObject.java

//This code is provided "AS IS"

package example;

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class ExampleRemoteObject extends UnicastRemoteObject

implements ExampleRemote {

public ExampleRemoteObject() throws RemoteException {

super();

}

public String test() throws RemoteException {

System.out.println("inside test method in ExampleRemoteObject");

return "from test method";

}

}

Those two commandline Java programs as follows:

TestClient.java

//This code is provided "AS IS"

package example;

import java.rmi.AlreadyBoundException;

import java.rmi.RemoteException;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

public class TestClient {

public TestClient() {

try {

ExampleRemoteObject remoteObject = new ExampleRemoteObject();

Registry registry = LocateRegistry.getRegistry("localhost", 1099);

registry.bind("exampleremote", remoteObject );

} catch (RemoteException e) {

e.printStackTrace();

} catch (AlreadyBoundException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new TestClient();

}

}

The other program that should be called from another command prompt, this

way we can simulate environment with two different JVM and ClassLoader.

TestRMIClient.java

//This code is provided "AS IS"

package example;

import java.rmi.NotBoundException;

import java.rmi.RemoteException;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

public class TestRMIClient {

public TestRMIClient() {

try {

Registry registry = LocateRegistry.getRegistry("localhost",1099);

ExampleRemote exampleR = (ExampleRemote) registry.lookup("exampleremote");

System.out.println(exampleR.test());

} catch (RemoteException e) {

e.printStackTrace();

} catch (NotBoundException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new TestRMIClient();

}

}

One question may you be thinking is what is this RMI registry that is located

in the client programs with a HOST address as localhost and port as 1099?

This is nothing but the rmiregistry.exe program found under %JAVA_HOME%\bin

folder.

One thing to remember is that, before running this rmiregistry.exe program,

classpath environment should be having the folder to the

example.ExampleRemote class, or else you might get exception such as

Caused by: java.lang.ClassNotFoundException: example.ExampleRemote

If everything setup properly, then one may run TestClient to have this program

stays running till the other client program, that is TestRMIClient is run,

in another command prompt, to receive system out as follows:

java TestClient

inside test method in ExampleRemoteObject

java TestRMIClient

from test method

This shows that TestRMIClient is able to invoke test method from the

other TestClient running program and able to receive "from test method"

returned value from the test method.

1

2

3

4

5

6

7

8

No comments:

Post a Comment