Created
September 7, 2017 08:55
-
-
Save tsegismont/c85455d0699e9deca69cc366f8ad0a7f to your computer and use it in GitHub Desktop.
AWS AsyncHandlerAdpater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.AmazonWebServiceRequest; | |
import com.amazonaws.handlers.AsyncHandler; | |
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync; | |
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsyncClientBuilder; | |
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest; | |
import com.amazonaws.services.dynamodbv2.model.DescribeTableResult; | |
import io.vertx.core.AsyncResult; | |
import io.vertx.core.Context; | |
import io.vertx.core.Future; | |
import io.vertx.core.Handler; | |
import io.vertx.core.Vertx; | |
import java.util.Objects; | |
/** | |
* @author Thomas Segismont | |
*/ | |
public class AsyncHandlerAdpater<REQUEST extends AmazonWebServiceRequest, RESULT> implements AsyncHandler<REQUEST, RESULT> { | |
private final Context context; | |
private final Future<RESULT> future; | |
public AsyncHandlerAdpater(Handler<AsyncResult<RESULT>> handler) { | |
context = Vertx.currentContext(); | |
Objects.requireNonNull(context); | |
future = Future.<RESULT>future().setHandler(handler); | |
} | |
@Override | |
public void onError(Exception e) { | |
context.runOnContext(v -> future.fail(e)); | |
} | |
@Override | |
public void onSuccess(REQUEST request, RESULT result) { | |
context.runOnContext(v -> future.complete(result)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync; | |
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest; | |
import com.amazonaws.services.dynamodbv2.model.DescribeTableResult; | |
import io.vertx.core.AbstractVerticle | |
/** | |
* @author Thomas Segismont | |
*/ | |
public class MyVerticle extends AbstractVerticle { | |
private final AmazonDynamoDBAsync dynamoDB; | |
public MyVerticle(AmazonDynamoDBAsync dynamoDB) { | |
this.dynamoDB = dynamoDB; | |
} | |
@Override | |
public void start() throws Exception { | |
vertx.createHttpServer() | |
.requestHandler(req -> { | |
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName("myTableName"); | |
dynamoDB.describeTableAsync(describeTableRequest, new AsyncHandlerAdpater<>(ar -> { | |
if (ar.succeeded()) { | |
DescribeTableResult result = ar.result(); | |
String tableStatus = result.getTable().getTableStatus(); | |
req.response().end(tableStatus); | |
} else { | |
ar.cause().printStackTrace(); | |
req.response().setStatusCode(500).end("Internal server error"); | |
} | |
})); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment