Skip to content

Instantly share code, notes, and snippets.

@bytekast
Last active May 21, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytekast/d5a544f8cdcd327a12010100b2ba2d66 to your computer and use it in GitHub Desktop.
Save bytekast/d5a544f8cdcd327a12010100b2ba2d66 to your computer and use it in GitHub Desktop.
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}'])
}
}
}
}
@ricardolpd
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment