Skip to content

Migrating from vert.x 2.1.5 to vert.x 3.0.0

rgwch edited this page Jun 29, 2015 · 10 revisions

Since the API changed quite a bit, the migration is not trivial.


Let's begin with the simple stuff:

  • org.vertx.java.core.* is now io.vertx.core.*
  • JsonObject's putXXX(field) methods are now put(field)
  • JsonObject's getObject(field) is now getJsonObject(field)
  • Jsonbject's getArray(field) is now getJsonArray(field)
  • EventBus#registerHandler(address,handler) is now EventBus#consumer(address,handler)

More difficult to change:

  • EventBus#send(String address,JsonObject message, Handler<Message<JsonObject>>) is now EventBus#send(String address, JsonObject message, AsyncResultHandler<Message<JsonObject>>) or, easier to read and to write: send(address,message, result -> {})

Most difficult (in my opinion): The changes to the HTTPServer and the EventBus bridge. It's not quite documented, and it changed a lot.

Here is my setup in 2.1.5:

final JsonObject bridgeCfg = rootConfig.getObject("bridge");
HttpServer http = vertx.createHttpServer().setCompressionSupported(true);
if (bridgeCfg.getBoolean("ssl", true)) {
  String keystorePath = bridgeCfg.getString("keystore", System.getProperty("user.home") +"/.jkeys/keystore.jks");    
  http.setSSL(true).setKeyStorePath(keystorePath).setKeyStorePassword(bridgeCfg.getString("keystore-pwd"));
}
http.requestHandler(new HTTPHandler(bridgeCfg, eb));
SockJSServer sock = vertx.createSockJSServer(http);
sock.bridge(new JsonObject().putString("prefix", "/eventbus"), bridgeCfg.getArray("inOK"),bridgeCfg.getArray("outOK"));
http.listen(bridgeCfg.getInteger("port"));

This is the same in 3.0.0:

final JsonObject bridgeCfg = rootConfig.getJsonObject("bridge");
HttpServerOptions options = new HttpServerOptions().setCompressionSupported(true);
if (bridgeCfg.getBoolean("ssl", true)) {
  String keystorePath = bridgeCfg.getString("keystore", System.getProperty("user.home")
    + "/.jkeys/keystore.jks");
  JksOptions ksopt = new JksOptions().setPath(keystorePath).setPassword(bridgeCfg.getString("keystore-pwd"));
  options.setSsl(true).setKeyStoreOptions(ksopt);
}
HttpServer http = vertx.createHttpServer(options);
Router router=Router.router(vertx);
SockJSHandler sock = SockJSHandler.create(vertx);
BridgeOptions bridgeOptions = new BridgeOptions();
JsonArray inOk=bridgeCfg.getJsonArray("inOK");
for(Object jo:inOk){
  bridgeOptions.addInboundPermitted(new PermittedOptions((JsonObject)jo));
}
for(Object jo:bridgeCfg.getJsonArray("outOK")){
  bridgeOptions.addOutboundPermitted(new PermittedOptions((JsonObject) jo));
}
sock.bridge(bridgeOptions);
router.route("/eventbus/*").handler(sock);
HTTPHandler rootHandler=new HTTPHandler(bridgeCfg,vertx.eventBus());
router.route().handler(routingContext ->{
  rootHandler.handle(routingContext.request());
});
http.requestHandler(router::accept).listen(bridgeCfg.getInteger("port"));

Sad things:

  • Modules (Yes, the elements that made vert.x so special...) are gone. Away. Disappeared. If your program relied on modules, you have to reimplement all functionality again differently. Period.

  • Database access has changed a lot. Sql and mongo connectors are now part of the vert.x codebase (since modules are gone, see above)

I tried to get around these two issues that way:

  • Tear the modules apart and integrate their Verticles in the main program

  • make two simple Verticles for mimicking the old mysql-postgresql and mongo modules: Just translate the very same EventBus calls into the new connector access. This way, loose coupling and easy mocking of the database layers remain possible.


Probably I didn't get everything right, but this way, at least it works :)