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

A Better Factory

Now that we’ve implemented a factory, and the basic idea of managing servers in response to client’s actions doesn’t seem so fearsome, it’s time to think about automating the process. One of the points I’ve made more than once in this book is that the server should not rely on the client behaving correctly. This applies to arguments passed from the client to the server, which should always be validated, and even more strongly to the idea that clients will release a server when they’re done with it.

In this section, we’ll discuss how the factory is similar to some previously implemented pieces of code and use these to talk about how our factory can be improved.

Some Prior Art

In Chapter 12, we implemented an automatic locking mechanism for our servers. The basic idea can be summarized as follows:

More than one client may want to access a particular account at a time. However, since a client could be making multiple calls (e.g., getting the balance and then making a withdrawal) from within a transaction, we need a way to lock an account when a client accesses it and relinquish the lock when the client is done with it.

Our first pass at this was to simply add two more methods to the Account interface.

public void getLock(  ) throws RemoteException, LockedAccountException;
public void releaseLock(  ) throws RemoteException;

Here, we relied on the client application to call getLock( ) and releaseLock( ) at the appropriate times.

After we implemented this, we then reimplemented ...

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