Skip to content

Instantly share code, notes, and snippets.


InfoSec812/Exercise8.groovy Secret

Created Dec 20, 2016
What would you like to do?
import io.vertx.core.AsyncResult
import io.vertx.core.CompositeFuture
import io.vertx.core.Future
import io.vertx.core.json.JsonObject
import io.vertx.groovy.core.eventbus.Message
import io.vertx.groovy.ext.web.RoutingContext
import io.vertx.lang.groovy.GroovyVerticle
import static groovy.json.JsonOutput.toJson
class Exercise8 extends GroovyVerticle {
void start() {
Future eventVerticleFuture = Future.future()
Future anotherVerticleFuture = Future.future()
CompositeFuture.join(eventVerticleFuture, anotherVerticleFuture).setHandler(this.&deployHandler)
vertx.deployVerticle('groovy:EventVerticle.groovy', eventVerticleFuture.completer())
vertx.deployVerticle('groovy:AnotherVerticle.groovy', anotherVerticleFuture.completer())
void rootHandler(RoutingContext ctx) {
def msg = new JsonObject([path: ctx.request().path()])
def replyHandler = { AsyncResult<Message> reply -> this.replyHandler(ctx, reply) }
vertx.eventBus().send('event.verticle', msg, replyHandler)
void replyHandler(RoutingContext ctx, AsyncResult<Message> reply) {
def response = ctx.response()
.putHeader('Content-Type', 'application/json')
if (reply.succeeded()) {
.end(new JsonObject(reply.result().body()).encodePrettily())
} else {
.setStatusMessage('Server Error')
void deployHandler(CompositeFuture cf) {
if (cf.succeeded()) {
LoggerFactory.getLogger(Exercise8).info('Successfully deployed all verticles')
// If the EventVerticle successfully deployed, configure and start the HTTP server
def router = Router.router(vertx)
vertx.createHttpServer() // Create a new HttpServer
.requestHandler(router.&accept) // Register a request handler
.listen(8080, '') // Listen on
} else {
def range = 0..(cf.size() - 1)
range.each { x ->
if (cf.failed(x)) {
LoggerFactory.getLogger(Exercise8).error('Failed to deploy verticle', cf.cause(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.