Java Reference
In-Depth Information
is used to initialize the server object while initializeSimulation()
is used to initialize the simulation code within that server object. In many cases,
both duties could be combined into one. However, examining the collabora-
tion diagram in Figure 17.2 (where initializeSimulation() was called
receiveInput() ) shows that initializeSimulation() at sequence
number 4.1 occurs much later in the collaboration than initialize() at
sequence number 2. In a complex simulation the data known by the client at
the two different times in the collaboration sequences could become important,
so we keep the two methods separate.
In a technique that is useful to efficiently support multiple simultaneous clients,
the factory server keeps a pool of instantiated but uninitialized servers. This pool
is created when the factory starts up, long before any client exists. To build the
pool, the factory merely creates several instances of the Server class and keeps
them in a HashMap . Using such a pool improves response time by allowing the
factory to supply an already constructed server from the pool during a client's
call to getInstance() rather than incurring the potentially nontrivial cost of a
new server construction for each new client. Since the client credentials in the ID
parameter provided to the factory cannot be known at server construction time,
when the pool is initially created, the initialize() method on the server
interface provides a good opportunity to supply those client credentials. When
that client is finished, the server can be returned to the pool, awaiting use by
another client with different credentials.
The server pool technique is useful and straightforward but a bit beyond the
scope of this chapter where we want to provide a simple example that demonstrates
working client/server concepts unencumbered by concerns of user/client creden-
tials and pool creation and maintenance. Therefore, the example for this chapter
passes only a simple String initparam to the initialize() method. As
with the ID passed to the factory, for simplicity we won't actually do anything
with initparam other than log it.
There are three other factory methods that we have not yet discussed -
initializeSimulation() , start() , and retrieveData() . Referring
to the UML diagrams in Chapters 16 and 17, we see that initialize-
Simulation() is where the initial data for the simulation code is provided
and start() is where the simulation thread is actually begun. Then the client
calls retrieveData() periodically to retrieve the results from the simulation.
20.4 Server factory implementation
The server factory implementation is rather simple. From the FactoryInter-
face class shown above, its only remotely exposed method is getInstance() .
The ServerFactory class implements FactoryInterface and extends
UnicastRemoteObject . Since we put the implementation into the impl
Search WWH ::




Custom Search