13
votes

In the Amazon Web Services sdk for java there is the possibility to create two different clients for DynamoDB: sync and async. The two objects can be then passed to the constructor of DynamoDBMapper. So, you should be able to create two different kinds of DynamoDBMapper: sync mapper and async mapper.

My question is: how does the async mapper work? I can't find any method in the async mapper that returns a Future object. So, how could I ever run multiple query asynchronously if I must always wait the return value of any method of the async mapper?

Thanks

2
Good Question. I was wondering the same as well. I've created an async client and passed it to the DynamoMapper but I don't think that makes it an async call. The documentation is very unclear about this.agentx
Exactly... At the end I wrote an implementation of Callable<T> that uses the SyncMapper in method "call". Then, I run such implementation inside an Executor's thread that returns a Future<T> object, that can be used for retrieving the results of the query and for synchronization with other query results. That's working fine.Ulisse
What framework are you using for the Executor Thread? I'm writing a Spring MVC controller, that receives POST data and uses the mapper to save the data in dynamo. I'm not sure about creating threads..its a bad practice to create a thread for each POST request you get. Not sure how to proceed..I need an aysnc client with the mapper.agentx
Right now, for many reasons, I am using no framework for managing the Executor Thread. But, I did use Spring in other projects with great results. See static.springsource.org/spring/docs/3.2.x/…Ulisse
I exactly had the same question!Richeek

2 Answers

16
votes

The asynchronous DynamoDB client extends from the synchronous client, and provides new method names for the asynchronous operations that return Futures. Currently the DynamoDBMapper will always use the synchronous methods of whatever AmazonDynamoDB client that you pass in. We'll take this feedback as a feature request for asynchronous support using the mapper.

0
votes

This is now supported by the enhanced DynamoDB client that comes with aws-sdk-java-v2

First, create an async version of the EnhancedClient & Table objects:

DynamoDbEnhancedAsyncClient enhancedClient = DynamoDbEnhancedAsyncClient.create();
DynamoDbAsyncTable<Customer> customerTable =
enhancedClient.table("customers_table", TableSchema.fromBean(Customer.class));

Then, you can perform operations on it asynchronously:

CompletableFuture<Customer> result = customerTable.getItem(r -> r.key(customerKey));

For more details see asynchronous-operations