de.swm.gwt.client.mobile.network.AsyncCallbackWithTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swm-gwt-client Show documentation
Show all versions of swm-gwt-client Show documentation
Plain GWT Swm erweiterungen, auch zur benutzung in mobilen Geraeten
The newest version!
package de.swm.gwt.client.mobile.network;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* Callback das einen Tiemout besitzt.
*
* @param der Typ
* @author wiese.daniel
* copyright (C) 2011, SWM Services GmbH
*/
public class AsyncCallbackWithTimeout implements AsyncCallback {
/**
* Timeout in ms. *
*/
private static final int DEFAULT_TIMEOUT = 60000;
private boolean complete;
private Timer timer;
private Error timeout;
private final AsyncCallback wrapppedCallback;
/**
* Default constructor.
*
* @param callback der gewrappte callback
*/
public AsyncCallbackWithTimeout(AsyncCallback callback) {
this(callback, DEFAULT_TIMEOUT);
}
/**
* Default constructor.
*
* @param callback der gewrappte callback
* @param timeoutMillis der timeout
*/
public AsyncCallbackWithTimeout(AsyncCallback callback, int timeoutMillis) {
this.wrapppedCallback = callback;
this.complete = false;
timer = new Timer() {
@Override
public void run() {
onTimeout();
}
};
timer.schedule(timeoutMillis);
timeout = new TimeOutException();
}
@Override
public void onFailure(Throwable caught) {
if (!complete) {
complete = true;
timer.cancel();
}
wrapppedCallback.onFailure(caught);
}
@Override
public void onSuccess(T result) {
if (!complete) {
complete = true;
timer.cancel();
wrapppedCallback.onSuccess(result);
}
}
/**
* Methode um den Timeout zu erhoehen.
*
* @param expectedTimeNeeded die erwartete Zeit die der call noch brauchen wird.
*/
public void resetTimeout(Integer expectedTimeNeeded) {
if (!complete) {
timer.schedule(expectedTimeNeeded == null ? DEFAULT_TIMEOUT : expectedTimeNeeded.intValue());
}
}
/**
* Exception um Timeouts zu bemerken.
*
* @author wiese.daniel
* copyright (C) 2011, SWM Services GmbH
*/
public static class TimeOutException extends Error {
private static final long serialVersionUID = 1L;
private long startTime = System.currentTimeMillis();
/**
* Default constructor.
*/
public TimeOutException() {
}
public String getMessage() {
return "An operation timed out; this may be caused by a slow or unreliable network connection, a server outage, or a bug. (waited "
+ (System.currentTimeMillis() - startTime) + " ms)";
}
}
/**
* Wird vom Timer aufgerufen, wenn der call nicht wie erwartet zurueckkommt.
*/
public void onTimeout() {
if (!complete) {
complete = true;
this.onFailure(timeout);
}
}
}