Skip to content

Instantly share code, notes, and snippets.

@bsandhu
Created December 29, 2017 21:32
Show Gist options
  • Save bsandhu/f42888a46940ad232ec2c20d3883c3b4 to your computer and use it in GitHub Desktop.
Save bsandhu/f42888a46940ad232ec2c20d3883c3b4 to your computer and use it in GitHub Desktop.
Two Verticles with Event Bus
public class MainVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
ObjectMapper mapper = new ObjectMapper();
vertx.createHttpServer().requestHandler(req -> {
vertx.eventBus().send("db.read", "Test", res -> {
req.response()
.setStatusCode(200)
.putHeader("content-type", "text/html")
.end("<!DOCTYPE html><html><body>" +
"<h1>Vert.x worker " + Thread.currentThread().getName() + "</h1>" +
"<hr/>" +
"<h2>" +res.result().body() + "</h2>" +
"</body></html>");
});
}).listen(8080);
System.out.println("HTTP server started on port 8080");
}
}
public class DBVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
ObjectMapper mapper = new ObjectMapper();
vertx.eventBus().consumer("db.read", message -> {
try {
Thread.sleep(100);
message.reply(mapper.writeValueAsString(new User("First", "Second", "516-100-1001")));
} catch (InterruptedException | JsonProcessingException e) {
e.printStackTrace();
message.reply("Error reading from DB");
}
});
System.out.println("Started DB Verticle");
}
}
public class Start {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle("com.example.demo.MainVerticle");
vertx.deployVerticle("com.example.demo.DBVerticle",
new DeploymentOptions()
.setWorker(true)
.setInstances(1000)
.setWorkerPoolSize(1000)
.setWorkerPoolName("DBWorkerPool"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment