jQuery in Action, 3rd edition
The moose likes Distributed Java and the fly likes RMI: How to test if a user is online before connection to server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "RMI: How to test if a user is online before connection to server" Watch "RMI: How to test if a user is online before connection to server" New topic

RMI: How to test if a user is online before connection to server

Bob Robertson

Joined: May 16, 2004
Posts: 17
Hi All,

I am trying to experiement with RMI a little and have come up with a small program to test, but I need some advice from the group.

Right now, I have created the following:

A server
A local client console
A remote client console

I would like the remote console to only perform 2pcs of the functionality the full local client provides. Part of this functionality is to allow a user to record some information while offline. This is stored in a flat file. When the user is able to get back on the network, I want them to read from the flat file and synchronize the data to a database I've got written.

The question I have is, when main() is called on the client, if the user if offline, a number of exceptions will be thrown. I don't want this since I want to allow a user to work offline until they can synchronize. I've been told that programming by exception is a bad practice, so I would like opinions on how to test to see whether the user is online or not.

Thanks in advance!

[ May 21, 2004: Message edited by: Bob Robertson ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I think that
programming by exception is a bad practice
is overly broad. There is nothing wrong with using an exception to detect the fact that the user is offline.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

The point about Exceptions is that they're intended to handle exceptional conditions. They carry extra overhead over simple if/then logic tests because they have to package up the execution context and throw it back up the stack chain.

However, that overhead is trivial compared to something like setting up a network session.

Exceptions are Bad Practice where you're using them for trivial or often/repeated situations, but when you've got a show-stopping condition (and I think failure to connect can reasonbly classified as such), then use of Exceptions can make your code much cleaner and more reliable. In some cases, also more efficient, since higher-level services don't need to have "is this working?" tests coded. They'll know that things are working becuase otherwise the Exception would fly right past them.

An IDE is no substitute for an Intelligent Developer.
Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
You have to design the local console to:
1. Save the data in a file if not connected;
2. Send the data to the remote server if not connected.
This is similar to a mail client that store
the messages in the Outbox and sends them when able to connect
to the SMTP server.

It's perfectly normal for the networking API to throw exceptions
and this can occur in 2 situations:
- When you connect;
- When you send the data.

All the time your application "knows" if it is connected or not.
Let assume you would model the connection status using a 'connected'
flag. When starting the client you are unconnected.

When the server connect method is called:
doLookup(); //connect
connected = true;

if( fileIsAvailable )
sendFile(); // this can throw an exception

removeLocalFile(); // don't let any IOException get through
catch( IOException ex )
connected = false;

When the save method is called:
if( connected )
catch( IOException )
connected = false;
saveToFile(); // this may throw an exception that you have to report

Catalin Merfu<br /><a href="http://www.accendia.com" target="_blank" rel="nofollow">High Performance Java Networking</a>
I agree. Here's the link: http://aspose.com/file-tools
subject: RMI: How to test if a user is online before connection to server
It's not a secret anymore!