The Client Application Summary

Both host-name and port-number have default values. host-name defaults to localhost and port-number defaults to 1099. Thus, the following are all equivalent names: localhost:1099default printer localhostdefault printer :1099default printer default printer The batch file, rmiprinterserver.bat, consists of the following two commands: start rmiregistry start java com.ora.rmibook.chapter4.rmiprinter.applications.SimpleServ er 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.

4.5 The Client Application

Once the changes to the data objects have been made and the skeletons and stubs have been generated from the server, the networking part of the client application is a remarkably straightforward piece of code. Recall that our client application had the GUI shown in Figur e 4- 3 . Figure 4-3. Printerclient application GUI The only part of this thats changed is the ActionListener attached to the Print File button. And its much simpler: private class PrintFile implements ActionListener { public void actionPerformedActionEvent event { try { FileInputStream documentStream = new FileInputStream_fileChooser getSelectedFile ; DocumentDescription documentDescription = new DocumentDescriptiondocumentStream; New network code follows Printer printer = Printer Naming.lookupDEFAULT_PRINTER_NAME; printer.printDocumentdocumentDescription; } catch PrinterException printerExc eption{ .... } } ... } All this does is use a predetermined name, which must be the same name as the server used to bind, to locate an object inside the RMI registry. It then casts the object to the correct type the RMI registry interface, like many Java interfaces, returns instances of Object and invokes the printDocument method on the server. And thats it Weve finished reimplementing the socket-based printer server as an RMI application. In this code example, as in many of the examples in this book, the client and server must be located on the same machine. This is because the call to Naming.lookup simply used DEFAULT_PRINTER_NAME with no hostname or port number specified. By changing the arguments used in the call to Naming.lookup , you can turn the example into a truly distributed application.

4.6 Summary

In this chapter, weve gone over the basics of developing an RMI application in a cookbook-style way, in order to get acquainted with the basic structure and components of an RMI application. Consequently, we glossed over many of the details. However, the key points to remember are: • Simple RMI applications are, in fact, not much more complicated than single-process applications. • RMI includes reasonable default solutions for the common problems in building distributed applications serialization handles marshalling and demarshalling, the registry helps clients find servers, and so on. • Even when problems arise e.g., DocumentDescription , the code is remarkably similar to, and simpler than, the analogous socket code. • The conceptual cost to using RMI isnt all that high. In most cases, using RMI amounts to adding an extra layer of indirection to your code. • The application evolution problems mentioned in Chapt er 3 arent nearly so forbidding when using RMI. The default mechanisms, and the automatically generated stubs and skeletons, handle many application evolution problems nicely.

Chapter 5. Introducing the Bank Example

Now that weve seen two versions of the same application, one written using sockets and one written using RMI, its time to take a step back and look at the whole process of designing a distributed application. In order to do this, this chapter and the following five chapters all concentrate on a single shared example: a distributed banking system. In this chapter, well get things underway by talking about the system requirements of a distributed banking system, sketching a rough architecture for the application and discussing the problems that arise in