Exploring The Rmi Mechanism

Páginas: 36 (9000 palabras) Publicado: 10 de marzo de 2013
Chapter 15: Exploring the RMI Mechanism
Overview
If you have Java on the client and the server, you can use Remote Method Invocation (RMI) to communicate
between them. The client will have a piece of stub code that acts as a proxy for particular classes on the
server. The client interacts with this code as if it were the remote object itself. By this we mean that you use
the same methods,not that there aren't additional considerations that could affect your performance. You are
passing objects back and forth across the network: You should consider carefully what it is you are passing
and how frequently you are passing it.
In this chapter, we'll explore the RMI mechanism by presenting an example of having a remote Java client
interact with Java classes on the server. We'll lookat developing a distributed application first in a single
directory, then in two directories on a single machine, then on two different machines, and finally using the
RMI daemon. Each iteration of this example will introduce a new issue. Initially, nothing is really being
passed back and forth, as everything is in the same CLASSPATH. This approach will enable us to discuss
specific features ofRMI rather than introducing them all at once.

The Components of a Basic RMI Application
We'll begin our examination of RMI with an example that enables you to create each of the components in a
basic RMI application. In the next section, we will expand upon this example to create an actual distributed,
albeit small, application. For now, we will concentrate on creating the interface. Inthis case, you will pass in a
String called name, and the Greeter will respond with the String Hello name. Of course, you don't need a
distributed application to print a personalized greeting for a user. You can accomplish the same task using a
servlet or JSP as we mentioned in Chapter 3, "Creating Dynamic Content with Servlets," and Chapter 4,
"Using JavaServer Pages," even in an enterprisesetting. Because you have gotten so adept at greeting others
by name, this example will enable you to concentrate on the RMI implementation details.
After creating the remote interface Greeter, you will then create an implementation of it that will act as your
remote object. The convention is to name this class GreeterImpl. The final class that you will hand−code for
the server is GreeterMain.You'll use this to start up the Greeter service. Finally, you will need to write
WelcomeGuest to act as the client requesting the service.
Note The logistics of actually running an RMI application are slightly more complicated than those shown in
this section. We start with this example to clearly show the roles played by the different components. In
the next section, we will explain how tospecify the security, the codebase, and how to run the example
over a network. We have found that many online tutorials only include examples wherein all the files are
on a single machine in the same directory.
Place all the files in this example in the following directory, which we will refer to as the Greetings directory
for short:
C:\J2EEBible\rmi\Greetings

Even with four classes runningfrom a single directory, you will still need to start three processes to run the
example. (We will explain how to run this example at the end of this section.)
309

Chapter 15: Exploring the RMI Mechanism

The Remote interface
In order for the client and server to communicate, they have to agree on what messages may be
communicated. Frequently, you want to expose the methods that can beinvoked without sharing how they are
implemented. This is best accomplished in Java by means of an interface. In this example, the Greeter
interface will contain the single method greetByName(), which takes a String as an argument and returns
another String. The following is the entire Greeter.java file:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Greeter extends...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • The Crisis Basic Mechanism
  • Exploring The Use Of Slow Motion
  • The Market For "Lemons": Quality Uncertainty And The Market Mechanism
  • Exploring The Sulfur Nutrient Cycle Using The Winogradsky Column
  • Ejercicio RMI
  • Exploring argentina
  • Java rmi
  • Rmi Replicacion

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS