Java RMI exercise

This page will be used for the Java Remote Method Invocation (RMI) group exercise. Feel free to print it and refer to it later!

Hello World!

Start by following Sun's own guide to running Hello World in Java RMI. Don't run the Registry,Server, and Client in the same terminal window, though: have a separate one for each, so it is easier to see what happens in each process. Note: if you get exceptions about not finding the class files when you start the programs, you should try to supply the full absolute path as the codebase parameter (e.g. -Djava.rmi.server.codebase=file://$HOME/wherever-you-put-the-files in a *NIX environment). When you have completed the guide, you should be able to answer the following questions:

  1. What is the purpose of the RMI registry?
  2. What is the default port of the RMI registry? How do you change it?
  3. What happens if you run the Server, close it (use the interrupt signal, usually sent via a terminal using the key combination Control-C), and then run it again, without shutting down the Registry?

Modifying Hello World

We will now modify the Hello World application to understand interesting features of Java RMI. Perform the following exercises, and report your results.

  1. The behavior you just observed when you attempt to run the Server twice without shutting down the Registry is annoying! Fix it! Use the Java 6 API to figure out how.
  2. Logging is important in any application. However, logging using System.out.println() is a horrible practice. Download log4j, a proper logging tool. Adhere to the log4j manual when you start using the log4j tool (have the simplest BasicConfigurator configuration discussed in the Configuration section). Sprinkle logging statements all over your code, making sure to have a logging statement in the sayHello() method. Get familiar with the output -- what information does it provide, and what can you do to make this information even more useful to you during future debugging sessions? See if your ideas are already implemented in log4j!
  3. What happens if you throw RuntimeException in the sayHello() method? Which process gets it? Why does this behavior make sense? Once you've seen the results, restore the method to its prior state (it should be back at returning a value, not throwing exceptions).
  4. Modify the Server code to achieve the following:
    • The message to return to clients may be specified using Java Properties.
    • The Registry location (and port) may be specified using Java Properties.
    • Rather than "binding" a reference into the Registry, make the code issue a request to "rebind" the reference (perhaps this is how you fixed the Server reconnection problem?).
    Now, start an RMI Registry on one computer using a port number of your choosing (try to avoid collisions with other groups, so 31337 and the likes are not recommended). In one console window, start a Server that will return the string "foo". In another window, start another Server that will return the string "bar". Experiment with startup order of the Servers using the Client and observe what happens!
  5. Using the modified code from last exercise, repeat your experiments, but this time, attempt to run one of the Servers on another computer (i.e. some other computer than the one where the Registry is running). What happens? What does this imply for your practical assignment?
  6. Registries can be run inside a process as well. Use the Java 6 API to figure out how!
  7. Distributed systems require security measures! What are Java Security Policies and what is their relationship to Java RMI? What would you have to do to create and use a file that lets you specify the security policies and permissions? Implement the changes and see the results!

More information

Sun provides an RMI Trail with a less trivial program that shows how Java RMI may be used to divide the work of computation in a distributed system. It is very interesting, and well worth the read! It explains, in depth, how Java RMI works and provides what you need to fully understand how to use it. You will most likely want to refer back to it if you choose to work using Java RMI.

If Java Security Policies are not enough, it is possible to use the Secure Socket Layer (SSL) with Java RMI. Use your favorite search engine to find out how! While you will hardly use this capability for the assignment, it may be worth remembering for future use.

Lessons learned

We hope you have learned some things about using Java RMI! Your most important task at this point is to consider what the implications of the lessons you have learned during this sessions are for how you solve the assignment.

Understanding the tools you will use makes it much easier to create a design that works from the beginning!