by
[Help |
API Docs |
Short Course|
Exercises]
In this exercise you will create an RMI service that returns two objects to the client.
As in SimpleBankingSystem
you need to change the constant HOST_NAME to match
your computer's name on the network.
You will also extend the Hello interface
and HelloImpl class to send a Java
object MessageObject
from the server to the client.
While you are making these changes, take a careful look at
MessageObject.
It defines both a class variable and an object variable, which are both
incremented each time the class is instantiated.
By observing how these counters are incremented on the server
and client side you can observe that RMI sends object instance
information between Java Virtual
Machines1(JVMs), but not the static or class-level information.
The course material for this exercise is covered in
Parameters in RMI.
Prerequisites
Simple Banking
System
Skeleton Code
Hello.java
HelloImpl.java
MessageObject.java
RMIServer.java
RMIClient.java
Tasks
1. Add the getMessageObject method to the Hello
interface.
2. Create an implementation for the getMessageObject method in the
HelloImpl class.
3. Compile all of the class files for the server and its remote objects.
4. Create the stub and skeleton files for the remote
object implementation.
5. Start the RMI Server in its own DOS console.
6. Start the RMI Client in its own DOS console.
Where help exists, the task numbers above are linked to the
step-by-step help page.
Solution Source
Hello.java
HelloImpl.java
MessageObject.java
RMIServer.java
RMIClient.java
Demonstration
When the server is run in its DOS console, the output will be:
Registry created on host computer <your_computer_name>
on port 10002
Remote HelloService implementation object created
Bindings Finished, waiting for client requests.
Then when the client is run in its DOS console, the output will be:
HelloService lookup successful
The server says: Hello!
printing the message ("Hello!") you defined in your implementation
object.
As your client runs both the server console and the client
console it prints the instantiation messages from
MessageObject. The server console displays:
MessageObject: Class Number is #0 Object Number is #0
MessageObject: Class Number is #1 Object Number is #1
MessageObject: Class Number is #2 Object Number is #2
MessageObject: Class Number is #3 Object Number is #3
MessageObject: Class Number is #4 Object Number is #4
MessageObject: Class Number is #5 Object Number is #5
MessageObject: Class Number is #6 Object Number is #6
MessageObject: Class Number is #7 Object Number is #7
MessageObject: Class Number is #8 Object Number is #8
MessageObject: Class Number is #9 Object Number is #9
|
While the client console displays:
MessageObject: Class Number is #0 Object Number is #0
MessageObject: Class Number is #0 Object Number is #1
MessageObject: Class Number is #0 Object Number is #2
MessageObject: Class Number is #0 Object Number is #3
MessageObject: Class Number is #0 Object Number is #4
MessageObject: Class Number is #0 Object Number is #5
MessageObject: Class Number is #0 Object Number is #6
MessageObject: Class Number is #0 Object Number is #7
MessageObject: Class Number is #0 Object Number is #8
MessageObject: Class Number is #0 Object Number is #9
|
Notice the difference in the class numbers.
While the MessageObject instance
is moved between the server and the client, the two JVMs load
their own, independent copies of the class file.
The class variable is incremented on the server, but this is
not reflected on the client side.
Copyright 1996-2000 jGuru.com. All Rights Reserved.
_______
1 As used on this web site, the terms "Java
virtual machine" or "JVM" mean a virtual machine for the Java
platform.
|