ZooKeeperRemoteExecutor extends AbstractRemoteExecutor { @Override public <C extends Command<R>, R extends Serializable> void run(final C command, final NodeCommandExecutionListener<C> listener, final AsyncCallback<R> callback) { ZNode commandNode = rootNode.child(nodeId.getType().name().toLowerCase()).child(nodeId.getIdentifier()).child(command.getClass().getName()); ZNode queueNode = commandNode.child("queue"); ZNode resultNode = commandNode.child("result"); final ZNode outputNode = resultNode.createChild(znode().persistentSequential()); outputNode.addNodeWatcher(new Watcher() { @Override public void process(WatchedEvent event) { log.debug("command {} execution done", command); CommandExecutionResult result = outputNode.getObject(CommandExecutionResult.class); switch (result.getStatus()) { case SUCCEEDED: log.debug("success"); callback.onSuccess((R) result.getResult()); break; case FAILED: Throwable e = result.getException(); log.error("fail", e); callback.onFailure(e); break; default: throw new IllegalStateException("Unknown status"); } outputNode.removeWithChildren(); } }); queueNode.createChild( znode() .persistentSequential() .withDataObject(new QueueEntry<C, R>(command, listener, outputNode.getPath())) ); log.debug("command {} is ready to be executed", command); } ZooKeeperRemoteExecutor(NodeId nodeId, ZNode rootNode); @Override void run(final C command, final NodeCommandExecutionListener<C> listener, final AsyncCallback<R> callback);  }
@Test public void testScheduling() throws Exception { ZNode outputNode = mock(ZNode.class); when(resultNode.createChild(znode().persistentSequential())).thenReturn(outputNode); String path = "/out/1"; when(outputNode.getPath()).thenReturn(path); TestCommand command = new TestCommand(); QueueEntry<TestCommand, Serializable> entry = new QueueEntry<TestCommand, Serializable>(command, null, path); remoteExecutor.run(command, null, Async.doNothing()); verify(outputNode).addNodeWatcher(any(Watcher.class)); verify(queueNode).createChild(znode().persistentSequential().withDataObject(entry)); }