Wednesday, December 20, 2006

RMI tutorial on SUN

1. Start the rmiregistry at the default port 1099. (rmiregistry.exe)

2. Start the Local Http server for delivering class files between server-RMI and RMI-Client.

D:\work\MyWork\rmi\suntutorial>java examples.classServer.ClassFileServer 1185 D:\work\MyWork\rmi\suntutorial

3. Then start the ComputeEngine server using :

D:\work\MyWork\rmi\suntutorial>java -cp .\;compute.jar -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/ -Djava.rmi.server.hostname=pc-krsethur-in -Djava.security.policy=D:\work\MyWork\rmi\suntutorial\server.policy engine.ComputeEngine

4. Remove the client files from the suntutorial folder and place it in a folder named test under this suntutorial so that RMI will get the files using http from the client(thus, the above classpath doesn't include the client classes).

5. server.policy should have the line

grant {
permission java.net.SocketPermission "*:*", "listen,accept,connect";
};

for the binding of remote object to the registry(1099).

6. Now run the client as

D:\work\MyWork\rmi\suntutorial\test>java -cp .\;..\compute.jar; -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/test/ -Djava.security.policy=D:\work\MyWork\rmi\suntutorial\client.policy client.ComputePi pc-krsethur-in 45

Output
3.141592653589793238462643383279502884197169399

7. Client.policy should have similar line above(5)

8. here -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/test/ will be set because during client execution, we call executeTask on the proxy remote object. The remote object running in the RMI runtime will delegate to the task object. Now, the task object is still not present in the RMI runtime and will give class not found exception. To prevent that we set the codebase to http://pc-krsethur-in:1185/test/. Here pc-krsethur-in:1185 will deliver files present under D:\work\MyWork\rmi\suntutorial. However, the task class is present in D:\work\MyWork\rmi\suntutorial\test folder. Hence, we append the test/ in the URL for the codebase. Our local file server will serve the classes from D:\work\MyWork\rmi\suntutorial\test for RMI runtime during client execution. Similarly, when the server was started, during binding of remote object, the interface classes(Compute.class and Task.class) for the RMI runtime was served by -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/.

9. After a single execution of both server and client, the classes(Compute.class and Task.class) for the server execution and the class(Task.class) from client execution will be present in the RMI runtime and hence if the local file server is stopped at this time and the server is aborted and restarted, it will work fine without any error about class not found. Similarly if the client is run now also, the client will work fine, because the Task.class is already loaded in the RMI runtime.

10. Now if we stop the rmiregistry.exe(thus all the classes loaded in RMI runtime are gone) and restart the rmiregistry and then start the server or client(with the local file server stopped), they will fail with class not found exception.

No comments: