views:

123

answers:

5

How to find out if the internet connection is not available from your j2ee web app, since some portions of our UI will not work properly if an internet connection is not available. So instead of failing with a bad exception, I would like to fail gracefully.

+4  A: 

The only universal way to test if "the internet" is working is to attempt to retrieve the resource you want. Anything else potentially can give you misleading or unhelpful results under certain circumstances.

For instance, if you use some site that is "nearly always available" as a proxy for the internet working ... it might be down or inaccessible when the site you really want to talk to is up and accessible. Or vice versa. In the former case, your application might conclude that "the internet" is unavailable and not even try to access the site you want to access. In the latter case, your application's diagnosis that "the internet" is available is immaterial to accessing the site it really needs to talk to.

Stephen C
+1  A: 

You could try either retrieving the resource your after or another small item from the web that is almost guaranteed 100% up-time, i.e. http://www.google.com.au/favicon.ico or similar

Hugoagogo
+1  A: 

The question seems unclear. How will the UI work if the internet connection is not available? Unless you are accessing the UI from the same machine as that of the server or in an intranet.

Do you want to check if the internet connection is available on the client side (in browser)?

Like @Stephen said above, the only way to determine this is to obtain the remote resource you want. On the server side, you can check if the server that hosts your resources is reachable with a combination of these APIs InetAddress.getByName and InetAddress.isreachable

If you want to determine if your clients are online then check with method on the navigator object:

nagivator.onLine
naikus
+2  A: 

Typical advanced desktop apps that haven't been developed in the 19th century and that need an "Internet connection" have an Network status... menu which shows something like:

Manx Telecom   80.65.254.253
Cambridge, UK  131.111.8.46
US West Coast  169.232.56.224
Yahoo          etc.
etc.           etc.

Find a list of IP that have been known to respond to pings for years, put 10 of them in your software.

If, say, at least 3 of them answer to a ping you know with a very high level of certainty that your app has full Internet connectivity.

Typically these apps stops pinging the other IPs as soon as they find at least three IP answering to the ping (IOW: they only display the latency / IPs of the first three that did answer).

The advantage of doing such a check instead of lamely just trying to fetch "the resource you want" (should there be a unique resource you want) is that you can directly see if you have no Internet connection at all or just a problem issuing some particular resource on a particular server.

Later on, when you want to fectch a particular resource, you can display message such as :

  • you have access to the Internet, but server 'x' seems to be inaccessible

or:

  • you don't seem to be connected to the Internet

Which are two TOTALLY different things.

Webinator
A: 

So instead of failing with a bad exception, I would like to fail gracefully.

So catch the 'bad' exception and turn it into a graceful failure.

Some of the other answers here seem to me to be beside the point. Testing connectivity to a site you're never going to connect to is pointless. Just attempt the connections you want to make and handle failures gracefully.

EJP