Create a gist now

Instantly share code, notes, and snippets.

anonymous /Handler.java Secret
Created Aug 29, 2017

What would you like to do?
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