Ejemplo n.º 1
0
  /**
   * Submits a command via the session.
   *
   * @param command The command to submit.
   * @param <T> The command output type.
   * @return A completable future to be completed with the command output.
   */
  public <T> CompletableFuture<T> submit(Command<T> command) {
    if (!isOpen()) return Futures.exceptionalFuture(new IllegalStateException("session not open"));

    CompletableFuture<T> future = new CompletableFuture<>();
    context
        .executor()
        .execute(
            () -> {
              CommandRequest request;
              if (command.consistency() == Command.ConsistencyLevel.NONE) {
                request =
                    CommandRequest.builder()
                        .withSession(id)
                        .withSequence(0)
                        .withCommand(command)
                        .build();
              } else {
                request =
                    CommandRequest.builder()
                        .withSession(id)
                        .withSequence(++commandRequest)
                        .withCommand(command)
                        .build();
              }

              submit(request, future);
            });
    return future;
  }