import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- *
- */
public abstract class AbstractInstructionExecutor implements FutureCallback<Instruction> {
+
+ private static final class InstructionCallback implements FutureCallback<OperationResult> {
+
+ private final Instruction insn;
+
+ public InstructionCallback(final Instruction insn) {
+ this.insn = insn;
+ }
+
+ @Override
+ public void onSuccess(final OperationResult result) {
+ if (result.getFailure() != null) {
+ switch (result.getFailure()) {
+ case Failed:
+ case NoAck:
+ this.insn.executionCompleted(InstructionStatus.Failed, null);
+ break;
+ case Unsent:
+ this.insn.executionCompleted(InstructionStatus.Cancelled, null);
+ break;
+ default:
+ break;
+ }
+ } else {
+ this.insn.executionCompleted(InstructionStatus.Successful, null);
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable t) {
+ this.insn.executionCompleted(InstructionStatus.Failed, null);
+ }
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(AbstractInstructionExecutor.class);
private final SubmitInstructionInput input;
public static FailureCase schedule(final InstructionScheduler scheduler, final AbstractInstructionExecutor fwd) {
final ListenableFuture<Instruction> s;
-
try {
s = scheduler.scheduleInstruction(fwd.getInput());
} catch (final SchedulerException e) {
LOG.info("Instuction {} failed to schedule", e.getMessage(), e);
return new FailureCaseBuilder().setFailure(e.getFailure()).build();
}
-
Futures.addCallback(s, fwd);
return null;
}
public void onSuccess(final Instruction insn) {
if (insn.checkedExecutionStart()) {
final ListenableFuture<OperationResult> s = invokeOperation();
- Futures.addCallback(s, new FutureCallback<OperationResult>() {
- @Override
- public void onSuccess(final OperationResult result) {
- if (result.getFailure() != null) {
- switch (result.getFailure()) {
- case Failed:
- case NoAck:
- insn.executionCompleted(InstructionStatus.Failed, null);
- break;
- case Unsent:
- insn.executionCompleted(InstructionStatus.Cancelled, null);
- break;
- default:
- break;
- }
- } else {
- insn.executionCompleted(InstructionStatus.Successful, null);
- }
- }
-
- @Override
- public void onFailure(final Throwable t) {
- insn.executionCompleted(InstructionStatus.Failed, null);
- }
- });
+ Futures.addCallback(s, new InstructionCallback(insn));
}
}