feat(spanner): add sample codes and sample ITs for Cloud Spanner Queues. - #14203
feat(spanner): add sample codes and sample ITs for Cloud Spanner Queues.#14203finnzzf wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new QueueSample class and its corresponding integration test QueueSampleIT to demonstrate Spanner queue operations, including database creation, sending, receiving, acknowledging, and deleting messages using both Mutation and SQL APIs. The review feedback suggests improving exception handling in the sample code by propagating exceptions in createQueueDatabase instead of catching and swallowing them, which simplifies the code and ensures failures are properly propagated.
| static void createQueueDatabase(DatabaseAdminClient dbAdminClient, String instanceId, String databaseId) { | ||
| try { | ||
| System.out.println("Creating database with a queue..."); | ||
| Database database = | ||
| dbAdminClient | ||
| .createDatabase( | ||
| instanceId, | ||
| databaseId, | ||
| Collections.singletonList( | ||
| "CREATE Queue MyQueue (" | ||
| + " Id INT64 NOT NULL," | ||
| + " Payload BYTES(MAX) NOT NULL," | ||
| + ") PRIMARY KEY (Id), OPTIONS (receive_mode = 'PULL')")) | ||
| .get(); | ||
| System.out.println("Created database [" + database.getId() + "]"); | ||
| } catch (ExecutionException | InterruptedException e) { | ||
| System.err.println("Database creation failed: " + e.getMessage()); | ||
| } | ||
| } |
There was a problem hiding this comment.
In sample codes, it is generally preferred to propagate exceptions rather than catching and swallowing them with System.err.println. Since the main method already declares throws Exception, we can simplify createQueueDatabase by declaring throws ExecutionException, InterruptedException on the method signature. This removes boilerplate try-catch code and ensures that any database creation failure is properly propagated and fails the execution immediately.
static void createQueueDatabase(DatabaseAdminClient dbAdminClient, String instanceId, String databaseId)
throws ExecutionException, InterruptedException {
System.out.println("Creating database with a queue...");
Database database =
dbAdminClient
.createDatabase(
instanceId,
databaseId,
Collections.singletonList(
"CREATE Queue MyQueue ("
+ " Id INT64 NOT NULL,"
+ " Payload BYTES(MAX) NOT NULL,"
+ ") PRIMARY KEY (Id), OPTIONS (receive_mode = 'PULL')"))
.get();
System.out.println("Created database [" + database.getId() + "]");
}
Adding sample codes and tests for queues.