Distributed object applications need to
rmiregistry,
or the application can pass and return remote object references as
part of its normal operation.
An object becomes remote by implementing a remote interface, which has the following characteristics.
java.rmi.Remote.
java.rmi.RemoteException in its throws
clause, in addition to any application-specific exceptions.
RMI treats a remote object differently from a nonremote object when the object is passed from one virtual machine to another. Rather than making a copy of the implementation object in the receiving virtual machine, RMI passes a remote stub for a remote object. The stub acts as the local representative, or proxy, for the remote object and basically is, to the caller, the remote reference. The caller invokes a method on the local stub, which is responsible for carrying out the method call on the remote object.
A stub for a remote object implements the same set of remote interfaces
that the remote object implements. This allows a stub to be cast to any
of the interfaces that the remote object implements. However, this also
means that only those methods defined in a
remote interface are available to be called in the receiving virtual
machine.
The rest of this lesson walks through the steps to create a compute engine.
The novel aspect of the compute engine is that the tasks it runs do not
need to be defined when the compute engine is written. New kinds of
tasks can be created at any time and then given to the compute engine
to be run. All that is required of a task is that its class implement a
particular interface. Such a task can be submitted to the compute
engine and run, even if the class that defines that task was written
long after the compute engine was written and started. The code needed
to accomplish the task can be downloaded by the RMI system to the
compute engine, and then the engine runs the task, using the resources
on the machine on which the compute engine is running.
The ability to perform arbitrary tasks is enabled by the dynamic nature
of the Java platform, which is extended to the network by RMI. RMI
dynamically loads the task code into the compute engine's Java virtual
machine and runs the task without prior knowledge of the class that
implements the task. An application like this, which has the ability to
download code dynamically, is often called a behavior-based
application. Such applications usually require full agent-enabled
infrastructures. With RMI such applications are part of the basic
mechanisms for distributed computing on the Java platform.
Bhopal newsCreating Distributed Applications Using RMI
When you use RMI to develop a distributed application,
you follow these general steps.
Design and Implement the Application Components
First, decide on your application architecture and determine which
components are local objects and which ones should be remotely
accessible. This step includes:
Compile Sources and Generate Stubs
This is a two-step process. In the first step you use the
javac compiler to compile the source files, which contain
the implementation of the remote interfaces and implementations, the
server classes, and the client classes. In the second step you use the
rmic compiler to create stubs for the remote objects. RMI
uses a remote object's stub class as a proxy in clients so that clients
can communicate with a particular remote object.
Make Classes Network Accessible
In this step you make everything--the class files associated with the
remote interfaces, stubs, and other classes that need to be downloaded
to clients--accessible via a Web server.
Start the Application
Starting the application includes running the RMI remote object
registry, the server, and the client.
Building a Generic Compute Engine
This trail focuses on a simple yet powerful distributed application
called a compute engine. The compute engine, a remote object in the
server, takes tasks from clients, runs them, and returns any results.
The tasks are run on the machine where the server is running. This sort
of distributed application could allow a number of client machines to
make use of a particularly powerful machine or one that has specialized
hardware.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100