Search in sources :

Example 11 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.

the class AsyncReadOnlyTransactionExample method asyncReadOnlyTransaction.

static void asyncReadOnlyTransaction(DatabaseClient client) throws InterruptedException, ExecutionException, TimeoutException {
    ApiFuture<Void> finished1;
    ApiFuture<Void> finished2;
    ExecutorService executor = Executors.newFixedThreadPool(2);
    try (ReadOnlyTransaction transaction = client.readOnlyTransaction()) {
        try (AsyncResultSet resultSet = transaction.executeQueryAsync(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) {
            finished1 = resultSet.setCallback(executor, new ReadyCallback() {

                @Override
                public CallbackResponse cursorReady(AsyncResultSet resultSet) {
                    try {
                        while (true) {
                            switch(resultSet.tryNext()) {
                                case OK:
                                    System.out.printf("%d %d %s%n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2));
                                    break;
                                case DONE:
                                    return CallbackResponse.DONE;
                                case NOT_READY:
                                    return CallbackResponse.CONTINUE;
                                default:
                                    throw new IllegalStateException();
                            }
                        }
                    } catch (SpannerException e) {
                        System.out.printf("Error in callback: %s%n", e.getMessage());
                        return CallbackResponse.DONE;
                    }
                }
            });
        }
        try (AsyncResultSet resultSet = transaction.executeQueryAsync(Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"))) {
            finished2 = resultSet.setCallback(executor, new ReadyCallback() {

                @Override
                public CallbackResponse cursorReady(AsyncResultSet resultSet) {
                    try {
                        while (true) {
                            switch(resultSet.tryNext()) {
                                case OK:
                                    System.out.printf("%d %s %s%n", resultSet.getLong(0), resultSet.getString(1), resultSet.getString(2));
                                    break;
                                case DONE:
                                    return CallbackResponse.DONE;
                                case NOT_READY:
                                    return CallbackResponse.CONTINUE;
                                default:
                                    throw new IllegalStateException();
                            }
                        }
                    } catch (SpannerException e) {
                        System.out.printf("Error in callback: %s%n", e.getMessage());
                        return CallbackResponse.DONE;
                    }
                }
            });
        }
    }
    ApiFutures.allAsList(ImmutableList.of(finished1, finished2)).get(60L, TimeUnit.SECONDS);
    executor.shutdown();
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ExecutorService(java.util.concurrent.ExecutorService) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) SpannerException(com.google.cloud.spanner.SpannerException) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback)

Example 12 with ReadyCallback

use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.

the class AsyncReadUsingIndexExample method asyncReadUsingIndex.

// Execute a query asynchronously and process the results in a callback.
static void asyncReadUsingIndex(DatabaseClient client) throws InterruptedException, ExecutionException, TimeoutException {
    ApiFuture<Void> finished;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try (AsyncResultSet resultSet = client.singleUse().readUsingIndexAsync("Albums", "AlbumsByAlbumTitle", // Read all rows in a table.
    KeySet.all(), Arrays.asList("AlbumId", "AlbumTitle"))) {
        // Setting a callback will automatically start the iteration over the results of the query
        // using the specified executor. The callback will be called at least once. The returned
        // ApiFuture is done when the callback has returned DONE and all resources used by the
        // AsyncResultSet have been released.
        finished = resultSet.setCallback(executor, new ReadyCallback() {

            @Override
            public CallbackResponse cursorReady(AsyncResultSet resultSet) {
                try {
                    while (true) {
                        switch(resultSet.tryNext()) {
                            // OK: There is a row ready.
                            case OK:
                                System.out.printf("%d %s%n", resultSet.getLong(0), resultSet.getString(1));
                                break;
                            // DONE: There are no more rows in the result set.
                            case DONE:
                                return CallbackResponse.DONE;
                            // NOT_READY: There are currently no more rows in the buffer.
                            case NOT_READY:
                                return CallbackResponse.CONTINUE;
                            default:
                                throw new IllegalStateException();
                        }
                    }
                } catch (SpannerException e) {
                    System.out.printf("Error in callback: %s%n", e.getMessage());
                    return CallbackResponse.DONE;
                }
            }
        });
    }
    // This ApiFuture is done when the callback has returned DONE and all resources of the
    // asynchronous result set have been released.
    finished.get(30L, TimeUnit.SECONDS);
    executor.shutdown();
}
Also used : AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ExecutorService(java.util.concurrent.ExecutorService) SpannerException(com.google.cloud.spanner.SpannerException) ReadyCallback(com.google.cloud.spanner.AsyncResultSet.ReadyCallback)

Aggregations

AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)12 ReadyCallback (com.google.cloud.spanner.AsyncResultSet.ReadyCallback)12 CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)8 ExecutorService (java.util.concurrent.ExecutorService)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Test (org.junit.Test)6 SpannerException (com.google.cloud.spanner.SpannerException)5 ArrayList (java.util.ArrayList)5 ApiFuture (com.google.api.core.ApiFuture)3 SettableApiFuture (com.google.api.core.SettableApiFuture)3 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)3 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)3 Statement (com.google.cloud.spanner.Statement)3 Collections2 (com.google.common.collect.Collections2)3 Lists (com.google.common.collect.Lists)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 Executors (java.util.concurrent.Executors)3