O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

RMI Server

Problem

The client looks good on paper, but will be lonely without a server to talk to.

Solution

You need to write two parts for the server, an implementation class and a main method. These can be in the same class or separated for clarity.

Discussion

The server-side code has to do a bit more work; see the sidebar.

This implementation divides the server into the traditional two parts, a main program and an implementation class. It is just as feasible to combine these in a single class. The main program shown in Example 22-3 simply constructs an instance of the implementation and registers it with the lookup service.

Example 22-3. DateServer.java

package darwinsys.distdate; import java.rmi.*; public class DateServer { public static void main(String[] args) { // You may want a SecurityManager for downloading of classes: // System.setSecurityManager(new RMISecurityManager( )); try { // Create an instance of the server object RemoteDateImpl im = new RemoteDateImpl( ); System.out.println("DateServer starting..."); // Locate it in the RMI registry. Naming.rebind(RemoteDate.LOOKUPNAME, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required