• Post Reply Bookmark Topic Watch Topic
  • New Topic

RMI Factory object  RSS feed

Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm using RMI in my SCJD solution. My remote interface is RemoteDataClient and my remote object is RemoteDataClietImpl.
Originally, l planned to register an instance of RemoteDataClientImpl which clients would then obtain a reference to.
However as I've gone to a lot of trouble to ensure thread safety even when there are multiple instances of RemoteDataClientImpl, this seems a bit of a waste (as all clients would then be accessing the database via the single instance of this class).

I'm now planning instead to register (with the RMI registry) a factory instead. This will expose a single method such as

public Remote DataClient getDatabaseConnection()
return new RemoteDataClientImpl();

thereby providing each client with their own "personal" database connection.

Obviously the factory itself must be a remote object, i.e.

- The factory's interface must extend Remote
- The factory implementation class must extend UnicastRemoteObject (and the remote interface)
- The factory implementation class must be compiled with rmic

But what about the DataClient which the factory returns. Obviously it doesn't need to be registered with the RMI registry, but does it need to be a remote object? I strongly suspect it does, but would be grateful for confirmation.

Thanks in Advance,
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12152
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,

The short answer is: yes, your DataClient will need to be a Remote object.

The slightly longer answer is that your DataClient can be either a Serializable object or a Remote object. If it is serializable, the DataClient object will be serialized and sent to the client (which doesn't really help us for the assignment since the data file itself will still be on the server). If the DataClient is a Remote object then it can be called remotely. If it is neither Serializable or Remote then you will get an exception when you try to get it remotely - from memory it might be an unmarshalling exception with a cause being a not serializable exception. It is worth trying this just so you can see what the exception is - the exception is useful if you thought that you were implementing serializable, but doesnt seem to make much sense if you thought you were calling a remote object, so seeing what creates that exception and how to fix it is a good learning exercise.

Regards, Andrew
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!