/** * Children call this method to signal the program is completed. */ protected void complete() { complete(State.COMPLETED); }
@Override public final ListenableFuture<ProgramController> command(final String name, final Object value) { final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { doCommand(name, value); result.set(AbstractProgramController.this); } catch (Throwable t) { error(t, result); } }); return result; }
@Override public final ListenableFuture<ProgramController> resume() { LOG.trace("Resume program {}", programId); if (!state.compareAndSet(State.SUSPENDED, State.RESUMING)) { return Futures.immediateFailedFuture( new IllegalStateException("Resumption not allowed for " + name + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.resuming(); doResume(); state.set(State.ALIVE); result.set(AbstractProgramController.this); caller.alive(); } catch (Throwable t) { error(t, result); } }); return result; }
@Override public final ListenableFuture<ProgramController> suspend() { LOG.trace("Suspend program {}", programId); if (!state.compareAndSet(State.ALIVE, State.SUSPENDING)) { return Futures.immediateFailedFuture( new IllegalStateException("Suspension not allowed for " + programId + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.suspending(); doSuspend(); state.set(State.SUSPENDED); result.set(AbstractProgramController.this); caller.suspended(); } catch (Throwable t) { error(t, result); } }); return result; }
@Override public final ListenableFuture<ProgramController> stop() { LOG.trace("Stop program {}", programId); if (!state.compareAndSet(State.STARTING, State.STOPPING) && !state.compareAndSet(State.ALIVE, State.STOPPING) && !state.compareAndSet(State.SUSPENDED, State.STOPPING)) { return Futures.immediateFailedFuture( new IllegalStateException("Stopping not allowed for " + name + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.stopping(); doStop(); state.set(State.KILLED); result.set(AbstractProgramController.this); caller.killed(); } catch (Throwable t) { error(t, result); } }); return result; }
listeners.put(caller, cancellable); result.put(cancellable); caller.init(getState(), getFailureCause()); } else { result.put(existing);
/** * Force this controller into error state. * @param t The failure cause */ protected final void error(final Throwable t) { executor.execute(() -> error(t, null)); }
@Override public final ListenableFuture<ProgramController> resume() { LOG.trace("Resume program {}", programId); if (!state.compareAndSet(State.SUSPENDED, State.RESUMING)) { return Futures.immediateFailedFuture( new IllegalStateException("Resumption not allowed for " + name + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.resuming(); doResume(); state.set(State.ALIVE); result.set(AbstractProgramController.this); caller.alive(); } catch (Throwable t) { error(t, result); } }); return result; }
@Override public final ListenableFuture<ProgramController> suspend() { LOG.trace("Suspend program {}", programId); if (!state.compareAndSet(State.ALIVE, State.SUSPENDING)) { return Futures.immediateFailedFuture( new IllegalStateException("Suspension not allowed for " + programId + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.suspending(); doSuspend(); state.set(State.SUSPENDED); result.set(AbstractProgramController.this); caller.suspended(); } catch (Throwable t) { error(t, result); } }); return result; }
@Override public final ListenableFuture<ProgramController> stop() { LOG.trace("Stop program {}", programId); if (!state.compareAndSet(State.STARTING, State.STOPPING) && !state.compareAndSet(State.ALIVE, State.STOPPING) && !state.compareAndSet(State.SUSPENDED, State.STOPPING)) { return Futures.immediateFailedFuture( new IllegalStateException("Stopping not allowed for " + name + " in " + state.get())); } final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { caller.stopping(); doStop(); state.set(State.KILLED); result.set(AbstractProgramController.this); caller.killed(); } catch (Throwable t) { error(t, result); } }); return result; }
listeners.put(caller, cancellable); result.put(cancellable); caller.init(getState(), getFailureCause()); } else { result.put(existing);
/** * Force this controller into error state. * @param t The failure cause */ protected final void error(final Throwable t) { executor.execute(() -> error(t, null)); }
@Override public final ListenableFuture<ProgramController> command(final String name, final Object value) { final SettableFuture<ProgramController> result = SettableFuture.create(); executor.execute(() -> { try { doCommand(name, value); result.set(AbstractProgramController.this); } catch (Throwable t) { error(t, result); } }); return result; }
/** * Children call this method to signal the program is completed. */ protected void complete() { complete(State.COMPLETED); }