Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Increase the functionality in your distributed client/server apps

Create the most elegant communications and storage solutions for your client/server apps with RMI and object serialization

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
I've spread this series out over quite a long period of time (I must give my colleagues a chance to write, after all), so before we get started, you may want to review the previous columns in which we built and modified the Forum application.

In Part 1: Write your own threaded discussion forum, we built the 1.0.2 client application, and in Part 2: The communications and server components, we developed the client-side networking and the server for 1.0.2. In Part 3: Scale an application from two to three tiers with JDBC, we converted the server to a JDK 1.1 middleware layer that communicated with an SQL Server database via JDBC. The 1.0.2 client continued to communicate with the middleware layer via sockets. Take a moment to review these versions, and I'll wait for you here....

Introducing RMI and object serialization

Remote Method Invocation (RMI) is a new API offered in JDK 1.1 that allows for messaging between different Java virtual machines (JVMs), even if they are separated by a network. Although it's not as efficient as TCP sockets and is still a bit buggy, RMI is interesting because it's high-level, yet easy to use. We're going to use it to allow the Forum client to call methods on the Forum server across the network. Core RMI functionality relies heavily on object serialization, which we'll look at next, and is contained in the java.rmi and java.rmi.server packages.

Object serialization is a facility that enables objects to be "flattened" into and out of ObjectOutputStreams, so that they can be stored in a file, sent across a network, or any number of other things. This flattening is accomplished by attaching an ObjectOutputStream to an appropriate lower-level OutputStream, such as a FileOutputStream, and writing the object into it. An instance of ObjectInputStream is then used to resurrect the object from the corresponding lower-level InputStream. Object serialization is a wonderful capability because it enables us to deal with live objects as objects instead of having to constantly break them into parts manually to transport or store them. We're going to use object serialization to clean up the loading and saving of the Forum server's article database. Object serialization facilities are contained in the java.io package.

Moving the Forum to RMI

We'll now begin the process of moving the Forum app from sockets to RMI and implementing object serialization to store and retrieve the articles database. For the sake of simplicity, we'll use the code and functionality in part 2 of our series as the basis for the new version we're developing. First, let's take a more detailed look at what is involved in using RMI.

RMI in-depth
RMI is used to transport method calls from a local client to a remote RMI server, which is a special object located on a remote machine. The RMI server usually extends java.rmi.server.UnicastRemoteObject and must implement at least one programmer-defined interface that itself is derived from the java.rmi.Remote "marker" interface. The methods declared in the derived interface will be the methods that the RMI server will export.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • JavaSoft's RMI Overview and links http://www.javasoft.com/products/jdk/1.1/docs/guide/rmi/index.html
  • JavaSoft's RMI API http://www.javasoft.com/products/jdk/1.1/docs/api/Package-java.rmi.html
  • JavaSoft's Object Serialization Overview http://www.javasoft.com/products/jdk/1.1/docs/guide/serialization/index.html
  • JavaSoft's ObjectOutputStream Documentation http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.ObjectOutputStream.html#_top_
  • JavaSoft's ObjectInputStream Documentation http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.ObjectInputStream.html#_top_
  • JavaSoft's online tutorial provides information on threadsafety http://java.sun.com/docs/books/tutorial/java/threads/synchronization.html
  • Usenet group devoted to general Java programming topics comp.lang.java.programmer
  • Previous Step by Step articles