Objectives In this chapter, we discuss two pure Java mechanisms for distributed
computing: socket-based communication and remote method invocation (RMI).
The design and implementation of distributed applications is illustrated
using a stock ticker application. We also discuss two mechanisms for interfacing
Java applications with non-Java applications in distributed computing environments:
Java database connectivity (JDBC), and the Common Object Request Broker
Architecture (CORBA). This chapter serves as an introduction to developing
distributed applications in Java.
Contents 9.1 Socket-Based Communication
9.1.1 Design Case Studies
9.2 Remote Method Invocation
9.2.1 The Architecture
9.2.2 Using RMI
9.2.3 Design Case Studies
Example 9.1: A simple server application: EchoServer
This example illustrates the basic elements of a server application. This
server echoes each line of text it receives from its client. The client
can terminate the connection by sending a line says "BYE." Typically, a
server is supposed to run for a long time. So, a server usually contains
an infinite loop of some kind. In this case, each iteration of the loop
handles one client. Thus, this server can handle multiple clients sequentially,
(i.e., only one client at a time,) not simultaneously.
Example 9.2: A simple client application: EchoClient
This example illustrates the basic elements of a client application that
communicates with the echo server. This client contacts the echo server
(the EchoServer from above example), and sends 10 lines of text to the
echo server and prints out the data it receives from the server.
Example 9.3: A server application handling multiple clients
This example illustrates a server that uses threads to handle multiple
clients simultaneously. The behavior of this echo server is similar to
the simple echo server, except that each client is handled by a separate
thread. The ClientHandler class defines the threads that handle the clients.
Note that, the body of the run() method is essentially the same as body
of the loop of the echo server. The MultiEchoServer class is the server,
which creates a server socket. Whenever a client request is received, a
new ClientHandler object, which is a thread, is created and started.
Example 9.4: The "Counter" client-server application
This example illustrates a client implemented as an applet. This applet
shows the number of visits, or hits, of the web page which contains the
applet. When the server starts up, it attempts to opens data file named
Counter.dat to retrieve last count. The CounterServer class is the visitor
counter server. It must be running on the host where the web server, i.e.,
the HTTP server, is running.
The Counter class is the visitor counter applet. The applet communicates
with the server to retrieve the count. In the init() method, a client socket
is created. Remember that an applet can only communicate with a server
resides on the same host as the web server. The host name of the client
socket can only be the host of the document base.
Example 9.5: The "BroadcastEchoServer" Application
This is an example of an echo server that handles multiple clients simultaneously,
and broadcasts messages to all active clients. This example illustrates
how clients can interact with one another through the server. The behavior
of this echo server is similar to the echo server in Example 9.1.2, except
that this server will broadcast messages received from a client to all
other active clients. This allows its clients to "chat" on-line. The BroadcastClientHandler
class defines a thread that handle the clients. The BroadcastEchoServer
class is the server. It contains a set of active clients, which will be
used while broadcasting messages.
This example shows the basic elements and steps in building an RMI application.
We develop an RMI server and an RMI client. The server sends a string "Hello
from Venus!" to its clients. The client, which is an applet, displays the
string it receives from the server. The Hello interface defines the service
to be provided by the server. The HelloImpl class is the server that implements
the Hello interface. The HelloApplet class is the client.
This example shows how to modify a database table with JDBC. The program
modifies the structure of the Beetles table, updates the test record in
that table, displays the modified table contents, and then deletes the
test record.
This example extends the Order applet from Example 6.13 so that it includes
database functionality. In this version, the applet writes each order to
the Order database. The OrderDialog class, from Example 6.13, is replaced
by a new class JdbcOrderDialog.
This example is a case study; a stock quote server and stock ticker client
applet are created. The stock quote server maintains the current quotes
of the stocks of a list of companies. A random number generator is used
to simulate the fluctuation of the quotes. The stock ticker client applet
displays the quotes of a selected list of companies that are of interest.
The ticker symbols and quotes scroll across the ticker banner continuously.