Java Reference
In-Depth Information
working on a single machine, you can still test out RMI, accessing the server on localhost
rather than another host:
package com.oozinoz.remote;
import java.rmi.*;
public class ShowRocketClient
{
public static void main(String[] args)
{
try
{
Object o = Naming.lookup(
"rmi://localhost:5000/Biggie");
Rocket biggie = (Rocket) o;
System.out.println(biggie.getApogee());
}
catch (Exception e)
{
System.out.println(
"Exception while looking up a rocket:");
e.printStackTrace();
}
}
}
When this program runs, it looks up an object with the registered name of "Biggie." The class
that is serving this name is RocketImpl , and the object o that lookup() returns will be an
instance of RocketImpl_Stub class. The RocketImpl_Stub class implements the
Rocket interface, so it is legal to cast the object o as an instance of the Rocket interface.
The Rocket_Impl class subclasses a RemoteStub class that lets the object communicate
with a server.
To see the ShowRocketClient program in action, copy the Rocket and
RocketImpl_Stub classes to a client area:
d:\client> dir com\oozinoz\remote
Rocket.class
RocketImpl_Stub.class
ShowRocketClient.java
Running the ShowRocketClient program prints out the apogee of a "Biggie" rocket:
d:\client> javac com\oozinoz\remote\ShowRocketClient.java
d:\client> java com.oozinoz.remote.ShowRocketClient
820.0
Through a proxy, the getApogee() call is forwarded to an implementation of the Rocket
interface that is active on a server.
Search WWH ::




Custom Search