Java Reference
In-Depth Information
subpackage below
javatech.all20.server
, the beginning of
Server-
Factory
is:
package javatech.all20.server.impl;
// various imports
...
import javatech.all20.server.*;
public class
ServerFactory
extends UnicastRemoteObject
implements FactoryInterface
{
private static int fServerIndex
--
= 0;
public ServerFactory () throws RemoteException {}
We have omitted the various standard
java.rmi
imports for brevity. We do
show the
import javatech.all20.server.*
because it is necessary for
the compiler to see the
FactoryInterface
and
ServerInterface
def-
initions. The constructor, as shown, does nothing. However, as explained in
Chapter 18, the constructor must be present and must be declared to throw
RemoteException
since
UnicastRemoteObject
throws
RemoteExcep-
tion
.Weexplain the class variable
fServerIndex
--
when we discuss the
getInstance()
method below.
20.4.1 Automatically starting the RMI registry
Recall that in Chapter 18 we manually started the RMI registry using the
rmiregistry
tool from the Java 2 SDK. Then the server implementa-
tion's
main()
method instantiated the server and bound it into the registry
under a known name. Thus starting the server was a two-step process: start
rmiregistry
and start the server.
Here we demonstrate how to skip the first step by automatically starting the
RMI registry from within the server factory's
main()
method. Then all we need
to do to start the server running and listening for clients is to run the server factory
class.
The
java.rmi.registry.LocateRegistry
class has a
create-
Registry()
method that starts the registry on a specified port. Its use is simple:
try {
java.rmi.registry.LocateRegistry.createRegistry (port);
}
catch (RemoteException re)
{
System.err.println (
"
Could not create rmiregistry:
"
+ re);
System.exit (1);
}
Search WWH ::
Custom Search