Java RMI Homework Exercise

Java RMI Homework Exercise


OBJECTIVE

The objective of this exercise is to gain experience programming in the Java programming environment and to understand the steps in using Java Remote Method Invocation (RMI) for building a distributed application. By the end of this exercise you should understand the tradeoffs in using RMI as compared to local procedure calls, and you should understand differences and similarities between Remote Procedure Call (RPC)and RMI. You will also build a simple client/server application using Java RMI.

STEP 0: Preparation

  1. Verify that you have an account on talon.csce.uark.edu. Linh Ngo <lngo@uark.edu> is the account manager for the class. If you have trouble logging in to your account after 8/25/04, send him email.

  2. This exercise assumes that you have some preliminary experience with Java. If you have never programmed in Java then you should spend some time going through the tutorial on Learning the Java Language at http://java.sun.com/docs/books/tutorial/java.

  3. This exercise also assumes that you have had exposure to some basic Internet concepts. Some helpful references for this are at:

The steps of this exercise are:

  1. Read the introductory material on RMI and RPC from the handout from class and on-line references.

  2. Follow the instructions in the Java Tutorial on RMI to write an example Java RMI program.

  3. Write your own Java RMI application. You will do all of your programming for this assignment on talon.csce.uark.edu, but you may use other platforms for testing.

  4. Answer the post-exercise questions.

What to turn in: The code for your assignment should be in your account on talon, in a subdirectory entitled "JavaRMIExercise" or something equally obvious. I will grade your assignment by executing your code from talon. Turn in the answers to the questions on paper to me in class.

STEP 1: Read the introduction and background reading material

To start, read the following:

RPC is a technique for building distributed, client-server based applications. It extends the notion of conventional, or local procedure calling. With RPC the called procedure does not need to be in the same address space as the calling procedure. The two processes can be executing on the same computer or on different computers. Programmers using RPC do not need to understand the details of the interface with the network. Because RPC is a layer over the transport layer of the network, it separates the application from the physical and logical elements of the data communcations mechanisms. RPC is the predecessor to RMI.

RMI makes object function calls between Java Virtual Machines (JVMs) possible, even when the JVMs are located on separate computers. Put another way, one JVM can call methods that belong to an object that is stored in another JVM. While many RPC and similar systems only allow simple parameters, RMI allows any Java object type to be passed as a parameter. RMI even allows both client and server to dynamically load new object types and pass them as parameters! RMI abstracts the problem so that the program/client machine does not know or care whether the requested object is executed locally or on another JVM. RMI also provides security and cross-platform portability that requires a large amount of overhead using RPC, but is limited since it can only call methods written in Java.

Read the article by Waldo Waldo, J., "Remote Procedure Calls and Java Remote Method Invocation," IEEE Concurrency, vol. 6, no. 3, pp. 5-7, July 1998. Available at http://csce.uark.edu/~aapon/courses/gridcomputing/papers/waldo98.pdf, for a detailed comparison of RPC and RMI.

STEP 2: Following the instructions in the Java RMI tutorial

Follow the steps of the Java Tutorial on RMI, available at http://java.sun.com/docs/books/tutorial/rmi/overview.html to create a client/server program using RMI.

Notice! These will be different on talon than specified in the tutorial:

The following is a log of the tutorial. The headings correspond to the headings in the tutorial.

<<< Begin of log from talon.csce.uark.edu >>>

[username@talon JavaRMIExercise]$ pwd
/home/username/JavaRMIExercise
[username@talon JavaRMIExercise]$ ls
client  compute  engine
[username@talon JavaRMIExercise]$ ls  client/
ComputePi.java  Pi.java
[username@talon JavaRMIExercise]$ ls  compute/
Compute.java  Task.java
[username@talon JavaRMIExercise]$ ls  engine/
ComputeEngine.java

<<< Build a JAR File of Interface Classes >>>

[username@talon JavaRMIExercise]$ javac  compute/*.java
[username@talon JavaRMIExercise]$ jar  cvf  compute.jar  compute/*.class
adding: META-INF/ (in=0) (out=0) (stored 0%)
adding: META-INF/MANIFEST.MF (in=56) (out=56) (stored 0%)
adding: compute/Compute.class (in=238) (out=168) (deflated 29%)
adding: compute/Task.class (in=166) (out=138) (deflated 16%)
Total:
------
(in = 449) (out = 813) (deflated -81%)

<<< Build the Server Classes >>>

[username@talon JavaRMIExercise]$ cd  /home/username
[username@talon username]$ mkdir  public_html
[username@talon username]$ mkdir  public_html/classes/
[username@talon username]$ cp  JavaRMIExercise/compute.jar  public_html/classes/
[username@talon username]$ cd  /home/username/JavaRMIExercise/
[username@talon JavaRMIExercise]$ javac  -classpath  ./compute.jar  engine/ComputeEngine.java
[username@talon JavaRMIExercise]$ rmic34  -d  .  engine.ComputeEngine
[username@talon JavaRMIExercise]$ mkdir  /home/username/public_html/classes/engine
[username@talon JavaRMIExercise]$ cp  engine/ComputeEngine_*.class  /home/username/public_html/classes/engine/
[username@talon JavaRMIExercise]$ cd  /home/username/public_html/classes/
[username@talon classes]$ jar  xvf  compute.jar
created: META-INF/
extracted: META-INF/MANIFEST.MF
created: compute/
inflated: compute/Compute.class
inflated: compute/Task.class

<<< Build the Client Classes >>>

[username@talon ~]$ cd  /home/username/JavaRMIExercise
[username@talon ~/JavaRMIExercise]$ cp  compute.jar  /home/username/public_html/classes/
[username@talon ~/JavaRMIExercise]$ javac  -classpath  /home/username/public_html/classes/compute.jar  client/Pi.java
[username@talon ~/JavaRMIExercise]$ javac  -classpath  /home/username/JavaRMIExercise:/home/username/public_html/classes/compute.jar  -d  /home/username/public_html/classes/  client/myCalculator.java

<<< Start the Server >>>

[username@talon classes]$ cd  /home/username/JavaRMIExercise/
[username@talon JavaRMIExercise]$ rmiregistry  &
[1] 28650
[username@talon JavaRMIExercise]$ java  -Djava.server.codebase=http://talon.csce.uark.edu/~username/classes/  -Djava.rmi.server.hostname=talon.csce.uark.edu   engine.ComputeEngine
ComputeEngine bound

<<< Start the Client (in another terminal) >>>

[username@talon ~/JavaRMIExercise]$ java  -classpath  /home/username/JavaRMIExercise:/home/username/public_html/classes  -Djava.rmi.server.codebase=http://talon.csce.uark.edu/~username/classes/   client.ComputePi  talon.csce.uark.edu  8
3.14159265

<<< End Log from talon.csce.uark.edu>>>

STEP 3: Write your own Java RMI application

Using the above information, your own programming skills, and the web as a reference, complete the following. I recommend using talon.csce.uark.edu for the assignment:
  1. In a package called compute, create a compute interface that extends Remote as discussed in the tutorial ( http://java.sun.com/docs/books/tutorial/rmi/designing.html ).

  2. Create a second interface, type Task, which provides the way to start the work (also found at http://java.sun.com/docs/books/tutorial/rmi/designing.html ).

  3. Implement a class for the compute engine itself. It should be called ComputeEngine and should extend UnicastRemoteObject. It will implement the remote Compute interface. ( http://java.sun.com/docs/books/tutorial/rmi/implementing.html ).

    Note: Steps 1-3 do not require any code editing, except for replacing "host" with "talon.csce.uark.edu" in ComputeEngine.java.

  4. In a new package called client, create a class called myCalculator (see ComputePi.java for an example). myCalculator will determine which operand (add, subtract, multiply, or divide) to use. It should take the following arguments:

    client.myCalculator machinename operator value1 value2

    machinename -
    the machine where the program will execute
    operator -
    will be one of the following strings: "add", "subtract", "multiply", or "divide"
    value1 and value2 -
    non-zero numbers
    Example: client.myCalculator talon.csce.uark.edu subtract 1 2

    This should output the value of 1 - 2 which is -1.

    (see http://java.sun.com/docs/books/tutorial/rmi/client.html for client help)

    Also create the Calc class in the client package which will actually do the work (see Pi.java). ( http://java.sun.com/docs/books/tutorial/rmi/client.html )

  5. Compile the files using javac and run them.

    Use the command: javac file1.java file2.java or
    javac *.java for files that contain dependencies.

    NOTE: javac may return errors if you use it to compile the files separately because it cannot resolve their dependencies. You can also use the classpath command line option to specify where to look for dependencies.

STEP 4: Answer the post-exercise questions

  1. Describe at least four things that are different between a local method invocation (or procedure call) and what happens with RPC or RMI.
  2. Describe the assumptions of the Java RMI system and compare them to the assumptions of RPC or RMI as implemented in CORBA.
  3. Why does Waldo think that Java RMI is better than earlier systems? Do you think Java RMI is better than earlier systems? Explain your answers.
  4. How would Java RMI behave or respond in the presence of a network failure? Explain your answer.
  5. Just for fun (don't turn this last question in), answer the quiz questions at http://java.sun.com/developer/Quizzes/rmi/index.html

Also answer the following questions, which will help me to improve this assignment. Turn these answers in on a separate piece of paper without your name on it.
  1. How many hours did you spend on this assignment?

    Rate each of the following on a scale of 1-10 where 1 is "almost none" and 10 is "a whole lot"

  2. Rate your Java skills before the RMI programming assignment.
  3. Rate your Java skills now.
  4. Rate your prior experience with Java RMI.
  5. The assignment was reasonable, given your starting Java programming skills.
  6. The preliminary reading material was sufficient. If it was not, can you describe what was missing?
  7. The assignment was comprehensible.
  8. What would you change about this assignment?

Enjoy!


Last modified: Wed Aug 25 21:47:18 Central Daylight Time 2004