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, ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.