Skip to content

Instantly share code, notes, and snippets.

/Handler.java Secret

Created August 29, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e03d7625aa8632e8932203b1c7d785ba to your computer and use it in GitHub Desktop.
Save anonymous/e03d7625aa8632e8932203b1c7d785ba to your computer and use it in GitHub Desktop.
public abstract class Handler<T, U extends QueuedItem<T>> {
protected final void handleQueuedItem(
Id<U> id,
Function<T, Result> action,
Class<U> rowClass) {
try {
T item = inTransaction(dbConnection -> {
U row = dbConnection.get(rowClass, id);
if (row.getPolledAt() == null) {
row.setPolledAt(clock.get());
return item;
}
return null;
});
if (item != null) {
Result result;
try {
result = action.apply(item)
} catch (Exception exception) {
logError(exception);
result = new Failure(exception);
}
inTransaction(dbConnection -> {
U row = dbConnection.get(rowClass, id);
if (result.isSuccess()) {
row.setSucceededAt(clock.get());
} else {
row.recordFailure(clock.get(), result);
}
})
}
} catch (Exception exception) {
logError(exception);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment