Re: [vertx:37313] Using Aws DynamoDB in Vertx

251 views
Skip to first unread message

Thomas SEGISMONT

unread,
Sep 26, 2016, 7:27:42 AM9/26/16
to ve...@googlegroups.com
Hi,

According to this article https://aws.amazon.com/articles/Java/5496117154196801 their SDK also offers a callback based API. So you could use this and, in the callbacks, go back to Vert.x world with #runOnContext.

Regards,

2016-09-24 7:36 GMT+02:00 ms98765 <micah...@gmail.com>:
Hi, I am interested in using DynamoDB from my Vertx verticle.  I've read other posts here but I'm unsure of what I should do to use their async java sdk from vertx without using executeBlocking.  I notice they use java.util.concurrent based Futures for the async.  Not sure that will help me much since I can't assign a callback handler.  Has anybody else ran into something similar or have any suggestions of the best way to make this work in a Vertx-friendly way (non-blocking)?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/9d8e5a03-ec42-491a-a8ab-7894bbd72496%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ms98765

unread,
Sep 26, 2016, 8:14:03 PM9/26/16
to vert.x
I'll take a look. Thanks!

ms98765

unread,
Oct 2, 2016, 9:14:32 AM10/2/16
to vert.x
It seems I can specify the future in my route handler and use that in the 3rd party callback without switching contexts.  Am I missing something?  I may be.

In the documentation on the AWS site, it states: Keep in mind though that your implementation of AsyncHandler is executed inside the thread pool owned by the asynchronous client.

To test this, I set up a vert.x periodic timer that fires every 100ms, and a route I can call from my browser to invoke the test function

Here are the methods used in test function:
public void testMe(RoutingContext rc)
{
Future<DescribeTableResult> myFuture = Future.future();
myFuture.setHandler(ar -> {
 if (ar.failed()) {
 
rc.response().end("Unexpected failure");
 } 
 else {
 
rc.response().end(ar.result().getTable().toString());
}
});
// invoke async method with the handler
test(myFuture);
say("End of testMe function reached");
}

public void test(Future<DescribeTableResult> future)
{
  dynamoDB.describeTableAsync(new DescribeTableRequest().withTableName("FakeTableName"), 
new AsyncHandler<DescribeTableRequest,DescribeTableResult>() {
public void onSuccess(DescribeTableRequest request, DescribeTableResult result) {
blockingSimulation();
future.complete(result);
say("Future is now complete for describing the table");
}
public void onError(Exception exception) {
future.failed();
say("Error describing table: " + exception.getMessage());
}
});
  
  say("End of test function reached");
}
  
private void blockingSimulation() {
int i = 0;
 
 say("Start blocking simulation");
 
 // simulate blocking action
 while (i++ < 5000000)
 {
if (i % 5 > 0)
{
String a = "one";
String b = " two";
String c = a + b + " three";
}
if (i % 7 > 0)
{
String d = "four";
String e = " five";
String f = d + e + " six";
}
 }
 
 say("Stop blocking simulation");
}


Here are the results in my console (and the correct results about the table are also returned to my browser window):

Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
End of test function reached
End of testMe function reached
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Start blocking simulation
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Stop blocking simulation
Future is now complete for describing the table
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0
Timer 1 fired: 0


On Monday, September 26, 2016 at 6:27:42 AM UTC-5, Thomas Segismont wrote:
Hi,

According to this article https://aws.amazon.com/articles/Java/5496117154196801 their SDK also offers a callback based API. So you could use this and, in the callbacks, go back to Vert.x world with #runOnContext.

Regards,
2016-09-24 7:36 GMT+02:00 ms98765 <micah...@gmail.com>:
Hi, I am interested in using DynamoDB from my Vertx verticle.  I've read other posts here but I'm unsure of what I should do to use their async java sdk from vertx without using executeBlocking.  I notice they use java.util.concurrent based Futures for the async.  Not sure that will help me much since I can't assign a callback handler.  Has anybody else ran into something similar or have any suggestions of the best way to make this work in a Vertx-friendly way (non-blocking)?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Thomas SEGISMONT

unread,
Oct 3, 2016, 4:15:15 AM10/3/16
to ve...@googlegroups.com
You can reply straight from the AWS thread pool, HttpServer classes are thread safe. But if you want maximum performance, you should make sure that they're always used by the same thread (by using #runOnContext)

To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages