Last active
May 21, 2019 18:11
-
-
Save bytekast/d5a544f8cdcd327a12010100b2ba2d66 to your computer and use it in GitHub Desktop.
This file contains 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
package com.serverless | |
import com.amazonaws.services.lambda.runtime.Context | |
import com.amazonaws.services.lambda.runtime.RequestHandler | |
import io.vertx.core.AbstractVerticle | |
import io.vertx.core.Vertx | |
import java.util.concurrent.CompletableFuture | |
import java.util.concurrent.TimeUnit | |
class VertxHandler implements RequestHandler<Map, Map> { | |
// Initialize Vertx instance and deploy UserService Verticle | |
final Vertx vertxInstance = { | |
System.setProperty('vertx.disableFileCPResolving', 'true') | |
final vertx = Vertx.vertx() | |
vertx.deployVerticle(UserService.newInstance()) | |
return vertx | |
}() | |
@Override | |
Map handleRequest(Map input, Context context) { | |
final future = new CompletableFuture<Map>() | |
// Send message to event bus using httpmethod:resource as dynamic channel | |
final eventBusAddress = "${input.httpMethod}:${input.resource}" | |
vertxInstance.eventBus().send(eventBusAddress, input, { asyncResult -> | |
if (asyncResult.succeeded()) { | |
future.complete(asyncResult.result().body()) | |
} else { | |
future.completeExceptionally(asyncResult.cause()) | |
} | |
}) | |
future.get(5, TimeUnit.SECONDS) | |
} | |
class UserService extends AbstractVerticle { | |
@Override | |
void start() throws Exception { | |
final eventBus = vertx.eventBus() | |
eventBus.consumer('GET:/users') { message -> | |
// Do something with Vert.x async, reactive APIs | |
message.reply([statusCode: 200, body: 'Received GET:/users']) | |
} | |
eventBus.consumer('POST:/users') { message -> | |
// Do something with Vert.x async, reactive APIs | |
message.reply([statusCode: 201, body: 'Received POST:/users']) | |
} | |
eventBus.consumer('GET:/users/{id}') { message -> | |
// Do something with Vert.x async, reactive APIs | |
message.reply([statusCode: 200, body: 'Received GET:/users/{id}']) | |
} | |
eventBus.consumer('PUT:/users/{id}') { message -> | |
// Do something with Vert.x async, reactive APIs | |
message.reply([statusCode: 200, body: 'Received PUT:/users/{id}']) | |
} | |
eventBus.consumer('DELETE:/users/{id}') { message -> | |
// Do something with Vert.x async, reactive APIs | |
message.reply([statusCode: 200, body: 'Received DELETE:/users/{id}']) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, i cant get paths like 'DELETE:/users/{id}' working with a java example. do you have any documentation that explains how to use parameters in eventbus addresses, i have spent a few hours today and i couldn't find anything.