45 lines
1.4 KiB
Java
45 lines
1.4 KiB
Java
package meerkat.bulletinboard.callbacks;
|
|
|
|
import com.google.common.util.concurrent.Futures;
|
|
import com.google.common.util.concurrent.ListeningExecutorService;
|
|
import meerkat.bulletinboard.AsyncBulletinBoardClient.*;
|
|
import meerkat.bulletinboard.BulletinClientJob;
|
|
import meerkat.bulletinboard.BulletinClientJobResult;
|
|
import meerkat.bulletinboard.BulletinClientWorker;
|
|
|
|
|
|
/**
|
|
* This is a future callback used to listen to workers and run on job finish
|
|
* Depending on the type of job and the finishing status of the worker: a decision is made whether to retry or return an error
|
|
*/
|
|
public class PostMessageFutureCallback extends ClientFutureCallback {
|
|
|
|
private ClientCallback<?> callback;
|
|
|
|
public PostMessageFutureCallback(ListeningExecutorService listeningExecutor,
|
|
ClientCallback<?> callback) {
|
|
super(listeningExecutor);
|
|
this.callback = callback;
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(BulletinClientJobResult result) {
|
|
|
|
BulletinClientJob job = result.getJob();
|
|
|
|
job.decMaxRetry();
|
|
|
|
// If redundancy is below threshold: retry
|
|
if (job.getMinServers() > 0 && job.isRetry()) {
|
|
Futures.addCallback(listeningExecutor.submit(new BulletinClientWorker(job)), this);
|
|
}
|
|
|
|
callback.handleCallback(null);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable t) {
|
|
callback.handleFailure(t);
|
|
}
|
|
}
|