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();
}
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();
}
Aggregations