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

Implementing the Server

We need to make one RMI-related decision when building our server. We have to decide whether to implement our server objects by subclassing UnicastRemoteObject.

UnicastRemoteObject is a class defined in the java.rmi.server package and is intended to be the generic superclass for RMI servers. Simply subclassing UnicastRemoteObject and implementing the appropriate remote interfaces is the simplest and most convenient way of building an RMI server.

A Server That Extends UnicastRemoteObject

Example 8-1 shows an implementation of Account that subclasses UnicastRemoteObject. One particularly important point is that none of the code, with the possible exception of the class declaration, has anything to do with the network. To an astonishing extent, this is ordinary Java code.

Tip

It is traditional in client-server circles to give interfaces descriptive names and then tack on an _Impl for the implementation. Thus, we have an interface called Account and an implementation called Account_Impl. This isn’t a particularly pleasant naming convention, but it is traditional.

Example 8-1. Account_Impl.java

public class Account_Impl extends UnicastRemoteObject implements Account { private Money _balance; public Account_Impl(Money startingBalance) throws RemoteException { _balance = startingBalance; } public Money getBalance( ) throws RemoteException { return _balance; } public void makeDeposit(Money amount) throws RemoteException, NegativeAmountException { checkForNegativeAmount(amount); ...

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