Using the RMISocketFactory Flexible Lookup Mechanism


This page will show you how to use a new feature of RMI that allows your application to choose which subclass of RMISocketFactory to use at runtime, based on: One of the primary benefits of the flexible lookup mechanism, is that you can use your custom socket factory in an applet, as long as that factory has been installed locally on the applet client machine, in the CLASSPATH of the JVM in which the applet is executing.

Before this feature was added, it was possible to use different types of sockets for different connections within the same JVM, but you had to write an RMISocketFactory that supported all of the socket types used by your application, as described by the tutorial on  Creating a Custom RMISocketFactory. You also had to set that particular RMISocketFactory as the default socket factory at the beginning of your RMI programs (for more information, see the tutorial on Creating a Custom Socket Type). Unfortunately, applets are not allowed to call the setSocketFactory method because it would apply to all applets executing in that JVM, so this new feature allows a different socket factory to be used on a per-object basis.

This tutorial will use another version of the "Hello World" example, modified to use a custom RMISocketFactory. The main difference between this example and the example used in the tutorial on creating a custom RMISocketFactory is that in this example, the RMISocketFactory is not set at the beginning of HelloImpl or HelloClient.  The steps to take to enable the  flexible lookup mechanism are:

  1. Write a subclass of java.rmi.server.RMISocketFactory that produces the types of sockets used by your application, named "RMISocketFactory".
  2. Decide on a name for the protocol that the sockets, produced by your RMISocketFactory,  will use for communication. Use this name as:
  3. Put your subclass of java.rmi.server.RMISocketFactory into a package.  Specifically, there should be a file called RMISocketFactory.java in the package chosen in the previous step, so the fully package-qualified name of your class will be in the format: <yourpackage>.<yourprotocol>.RMISocketFactory
  4. Set your CLASSPATH so that the RMI runtime can find the custom RMI socket factories used by your remote application.
  5. Run the server and the client.