O'Reilly logo

Java RMI by William Grosso

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

The Rest of the Server

To finish building our server, we need to write launch code. Launch code is code that is application-specific, but not business-domain specific, and handles the details of registering a server with a naming service such as the RMI registry. In our case, this boils down to two pieces of code: a Java program that runs PrinterServer and a batch file that starts the RMI registry and then runs our program. The former is shown in Example 4-5.

Example 4-5. SimpleServer.java

public class SimpleServer implements NetworkConstants {
	public static void main(String args[]) {
		try {
			File logfile = new File("C:\\temp\\serverLogfile");
			OutputStream outputStream = new FileOutputStream(logfile);
			Printer printer = new NullPrinter(outputStream);
			Naming.rebind(DEFAULT_PRINTER_NAME, printer);
		}
		catch (Exception e) {
			e.printStackTrace(  );
		}
	}
}

This creates an instance of NullPrinter and then binds it into the registry under the name DEFAULT_PRINTER_NAME. The only surprising detail is this: if everything is successful, our program will exit main( ). Don’t worry; this is normal. The fact that the RMI registry has a reference (e.g., a stub) for the server keeps the application alive even though we’ve exited. I’ll explain why, and how this works, in Chapter 16.

Note

Note that we used rebind( ) instead of bind( ) in our launch code. The reason is that bind( ) fails if the name we’re binding the server to is already in use. rebind( ), on the other hand, is guaranteed to succeed. If another server is bound into the registry using the name we want to use, that server will be unbound from the name. In reality, bind( ) is rarely used in launch code, but is often used in code that attempts to repair or update a registry.

The format of names that are bound into the registry is fairly simple: they follow the pattern //host-name:port-number/human-readable-name. host-name and port-number are used to find the registry.

The batch file, rmiprinterserver.bat, consists of the following two commands:

start rmiregistry
start java com.ora.rmibook.chapter4.rmiprinter.applications.SimpleServer

start is a Windows command that executes the rest of the line in a separate process. It is equivalent to putting an ampersand (&) after a command in a Unix shell. Thus, invoking rmiprinterserver.bat from the DOS shell launches the RMI registry in another process, launches SimpleServer in a third process, and then returns to the command prompt to wait for further instructions.

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