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

Incorporating a Custom Socket into an Application

Now that we’ve got a custom socket factory, the next step is incorporating it into our application. There are two different ways to do this: we can either modify our server classes or we can set RMI’s default socket factory. The first option, modifying individual server classes, gives us a very fine-grained level of control in which each server can use a different custom socket class. Setting RMI’s default socket factory is a much coarser way to do things. Changing the default socket factory means that any servers that don’t set their own socket factories will use the new default types.

Modifying Ordinary Servers

UnicastRemoteObject has three constructors:

protected UnicastRemoteObject(  ) 
protected UnicastRemoteObject(int port) 
protected UnicastRemoteObject(int port, RMIClientSocketFactory csf,
    RMIServerSocketFactory ssf)

Until this point, we’ve used only the first two of these in our servers. In fact, we’ve mostly used the first one, as in the following code snippet:

public class Account_Impl extends UnicastRemoteObject implements Account {
	private Money _balance;
	public Account_Impl(Money startingBalance) throws RemoteException {
		_balance = startingBalance;
	}
	//...
}

This is a perfectly reasonable thing to do; the zero-argument constructor gives the RMI runtime the ability to reuse an existing server socket, whereas the one-argument constructor forces the RMI runtime to create a server socket that listens on the specified port. ...

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