Search in sources :

Example 1 with StatusClosable

use of io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable in project ksql by confluentinc.

the class Cli method handlePrintedTopic.

@SuppressWarnings({ "try", "unused" })
private void handlePrintedTopic(final String printTopic, final SqlBaseParser.PrintTopicContext ignored) {
    final RestResponse<StreamPublisher<String>> topicResponse = makeKsqlRequest(printTopic, restClient::makePrintTopicRequest);
    if (topicResponse.isSuccessful()) {
        try (StatusClosable toClose = terminal.setStatusMessage("Press CTRL-C to interrupt")) {
            final CompletableFuture<Void> future = new CompletableFuture<>();
            final StreamPublisher<String> publisher = topicResponse.getResponse();
            final PrintTopicSubscriber subscriber = new PrintTopicSubscriber(publisher.getContext(), future);
            publisher.subscribe(subscriber);
            terminal.handle(Terminal.Signal.INT, signal -> {
                terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
                subscriber.close();
                future.complete(null);
            });
            try {
                future.get();
            } catch (Exception e) {
                LOGGER.error("Unexpected exception in waiting for print topic completion", e);
            } finally {
                publisher.close();
            }
            terminal.writer().println("Topic printing ceased");
        }
    } else {
        terminal.writer().println(topicResponse.getErrorMessage().getMessage());
    }
    terminal.flush();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) VertxException(io.vertx.core.VertxException) KsqlRestClientException(io.confluent.ksql.rest.client.exception.KsqlRestClientException) EndOfFileException(org.jline.reader.EndOfFileException) ExecutionException(java.util.concurrent.ExecutionException) KsqlUnsupportedServerException(io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException) KsqlMissingCredentialsException(io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 2 with StatusClosable

use of io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable in project ksql by confluentinc.

the class JLineTerminalTest method shouldResetStatusMessage.

@Test
public void shouldResetStatusMessage() {
    // Given:
    final StatusClosable closable = terminal.setStatusMessage("test message");
    clearInvocations(statusBar);
    // When:
    closable.close();
    // Then:
    verify(statusBar).update(ImmutableList.of(new AttributedString("", AttributedStyle.DEFAULT)));
}
Also used : AttributedString(org.jline.utils.AttributedString) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) Test(org.junit.Test)

Example 3 with StatusClosable

use of io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable in project ksql by confluentinc.

the class Cli method handleQuery.

// ignored param is required to compile.
@SuppressWarnings({ "try", "unused" })
private void handleQuery(final String statement, final SqlBaseParser.QueryStatementContext query) {
    final RestResponse<StreamPublisher<StreamedRow>> queryResponse = makeKsqlRequest(statement, restClient::makeQueryRequestStreamed);
    if (!queryResponse.isSuccessful()) {
        terminal.printErrorMessage(queryResponse.getErrorMessage());
        terminal.flush();
    } else {
        try (StatusClosable toClose = terminal.setStatusMessage("Press CTRL-C to interrupt")) {
            final StreamPublisher<StreamedRow> publisher = queryResponse.getResponse();
            final CompletableFuture<Void> future = new CompletableFuture<>();
            final QueryStreamSubscriber subscriber = new QueryStreamSubscriber(publisher.getContext(), future);
            publisher.subscribe(subscriber);
            terminal.handle(Terminal.Signal.INT, signal -> {
                terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
                subscriber.close();
                future.complete(null);
            });
            try {
                if (streamedQueryTimeoutMs != null) {
                    future.get(streamedQueryTimeoutMs, TimeUnit.MILLISECONDS);
                } else {
                    future.get();
                }
            } catch (Exception e) {
                LOGGER.error("Unexpected exception in waiting for query", e);
            } finally {
                terminal.writer().println("Query terminated");
                terminal.flush();
                publisher.close();
            }
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) VertxException(io.vertx.core.VertxException) KsqlRestClientException(io.confluent.ksql.rest.client.exception.KsqlRestClientException) EndOfFileException(org.jline.reader.EndOfFileException) ExecutionException(java.util.concurrent.ExecutionException) KsqlUnsupportedServerException(io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException) KsqlMissingCredentialsException(io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException) UserInterruptException(org.jline.reader.UserInterruptException)

Aggregations

StatusClosable (io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable)3 StreamPublisher (io.confluent.ksql.rest.client.StreamPublisher)2 KsqlMissingCredentialsException (io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException)2 KsqlRestClientException (io.confluent.ksql.rest.client.exception.KsqlRestClientException)2 KsqlUnsupportedServerException (io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException)2 VertxException (io.vertx.core.VertxException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 EndOfFileException (org.jline.reader.EndOfFileException)2 UserInterruptException (org.jline.reader.UserInterruptException)2 StreamedRow (io.confluent.ksql.rest.entity.StreamedRow)1 AttributedString (org.jline.utils.AttributedString)1 Test (org.junit.Test)1