Schnellzugriff » 
Home 
« How-to: Debian: Automatically mounted loopback images with dm-crypt, LUKS, pam_mount Redmine, Apache, Subversion: Could not read status line »

Java: Periodically check internet connection availability

A very simple example of how to periodically check the availability of an internet connection by using the observer design pattern.

InternetConnection.java

package net;

import java.net.*;
import java.io.*;

public class InternetConnection {
	/*	use at least one reliable host, in most cases you will want
 		to use the one you're working with later	*/
	private static final String CHECK_URL = "http://www.t-online.de/";
	public static InternetConnectionState isAvailable() {
		try {
			URL url = new URL(InternetConnection.CHECK_URL);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			Object objData = urlConnection.getContent();
		} catch(UnknownHostException exception) {
			return new InternetConnectionState(false);
		} catch(IOException exception) {
			return new InternetConnectionState(false);
		}
		return new InternetConnectionState(true);
	}
}

InternetConnectionState.java

package net;

public class InternetConnectionState {
	private boolean state = false;

	public InternetConnectionState(boolean state) {
		this.state = state;
	}

	public boolean getState() {
		return this.state;
	}

	public void setState(boolean state) {
		this.state = state;
	}
}

InternetConnectionObserver.java

package net;

import java.util.Observable;
import java.util.Observer;

public class InternetConnectionObserver implements Observer {
	public InternetConnectionObserver(ObservableInternetConnection internetConnection) {
		internetConnection.addObserver(this);
		new Thread(internetConnection).start();
	}

	public void update(Observable internetConnection, Object state) {
		InternetConnectionState connectionState = (InternetConnectionState)state;
		if(connectionState.getState() == true) {
			System.err.println("active internet connection detected");
		} else {
			System.err.println("lost internet connection");
		}
	}
}

ObservableInternetConnection.java

package net;

import java.util.*;
import net.InternetConnectionState;

public class ObservableInternetConnection extends Observable implements Runnable {
	private static final long CHECK_INTERVAL = 30000; // 30 sec
	private InternetConnectionState state;

	public void run() {
		this.state = InternetConnection.isAvailable();

		this.updateConnectionStatus();
	}

	public void updateConnectionStatus() {
		while(true) {
			InternetConnectionState newState = InternetConnection.isAvailable();
			if(newState.getState() != this.state.getState()) {
				this.state = newState;
				setChanged();
				notifyObservers(this.state);
			}
			try {
				Thread.sleep(ObservableInternetConnection.CHECK_INTERVAL);
			} catch(InterruptedException e) {
			}
		}
	}
}

Usage:

ObservableInternetConnection observableInternetConnection = new ObservableInternetConnection();
InternetConnectionObserver internetConnectionObserver = new InternetConnectionObserver(observableInternetConnection);

…I just startet programming Java a few days ago so I’m pretty sure there’s a better way to do this but I didn’t find it, yet Icon Wink in Java: Periodically check internet connection availability

Freitag, Juni 25th, 2010 and is filed under Java, Programmierung. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

One Response to “Java: Periodically check internet connection availability”

  1. Edgar Ortega (1 comments) Says:
    April 18th, 2011 at 22:29

    Thanks!!! :D


© 2007 - 2009 Thorsten Boock