Kotlin and Vert.x



  I just started to try kotlin with vert.x

  I’ve heard some one would try it earlier, but then no news. So I start to do it myself now.

  Currently there is more interest in integration with Play!, but I’ve found that they have already stopped new dev on Play! 1.x branch (which is java and excellent), and Play! 2.0 is pure Scala implemented, which IMHO might lead to some trouble, so when I found about vert.x, think that would be a good fit for kotlin. Vert.x is Plotglot natively !

  I got the simplest HTTP hello world running OK now.

  Will try to integrate kool-template so that I can actually make a web app. (Unlike Play! There is no dedicated template system in vert.x yet).

  The code is in github if you are interested. https://github.com/zhaopuming/kert.x

  I heard that some one has looked into this in one post earlier, but
  I have some issues already:

  1. Should I just wrap around vert.x code?

  In vert.x java, there is no lambda, so they come up with a Hanlder<T> class used as a function, I've successfully wrapped some of there methods that takes a Handler<T> to a Kotlin function. Now the code looks like:

public override fun start() {   val server = this.getVertx()?.createHttpServer()!!   val routes  = RouteMatcher()

  routes.get("/", { (req) ->

  server.requestHandler(routes as Handler<HttpServerRequest?>)

  server.listen(8080, “localhost”)

  But there remains a lot of T? in the code, where we may still have to deal with nullability. Is this a issue? Is it a good practice to ingore nullability when wrapping a Java lib?

  2. for each methods that takes a Handler, I’ll have to make a duplicate to take a Kotlin function instead, but when using it we have to import that dup method. Is that the right way to go?

  3. I haven’t used kool-template in vert.x yet. but I suppose it has no dependency on other libs and have no issue with classpath, right ?

  Before thinking about kool-template, I was trying to integrate Rythm template (a good template engine in Play! framework), but in vain due to classpath problems. I have contacted Rythm’s author but he seems to be busy at the moment.

  4. other than  Hanlder wrapped with kotlin function and kool-template, I haven’t got any new ideas yet. What would you suggest.

  finally, I need help from the community. I’m new to kotlin and have not  much experience. Anyone interested in vert.x & kotlin integrated?


Great stuff! It'd be good to team up with Alex's vert.x stuff too: https://github.com/alextkachman/vertex-kotlin

Maybe we can merge efforts together? Also a nice ReadMe.md file in the root directory really helps folks get up to speed on a project.

Would be good to host online APIs docs too so folks can more easily grok how to use kotlin + vert.x together (e.g. like kool.io’s API docs which are created via the maven build then uploaded to github pages via the ‘mvn site:deploy’).

I’ll happily help getting Kool Templates working with vert.x if folks don’t beat me to it; anyone got any pointers to how to wrap up a template engine for vert.x?


Thanks James.

I didn’t know about alex’s stuff, If I did I wouldn’t try to make my own.

I’ll try with his code and see how I can contribute.

Do you know how I can contact him? I’ve briefed some of his code and found that its copyright under JetBrains, does that mean this is an official work?

and for your question about template, it seems vert.x doesn’t have any template system yet.

The simplest method would be just call the template in a controller. Isn’t that a kool-template is just a function?

Another solution would be Mods:

vert.x has a module system called Mods. See docs here

maybe we could just write a BusMod (A special kind of verticle) that takes messages from the EventBus and replies with kool-template’s output.

Then a front end controller will just send a message with parameters to this BusMod, and wait for its reply, and then sends the reply to HttpResponse.

But issue may occur because EventBus only accepts simple data types (Integer, String, etc) and JsonObject,not sure how that would affect performance for the template.


Yes, colaboration would be really great.

Regarding KoolTemplates I will do small example today.


Really funny how similar are our appoaches


I spotted Alex's work on the mainling list, but he's here now :)

Yeah, Kool Templates are just functions. They currently make W3C DOM trees (so they work nicely in the client side & with other XML / HTML tools and libraries etc) though its easy to turn the DOM into a String or whatnot.


BTW I wonder if its worth forking vert.x directly then adding vertx-lang-kotlin as a module: https://github.com/vert-x/vert.x/tree/master/vertx-lang

then with a pull request the kotlin version could become part of the standard distro?

Though I guess it could be a separte, stand alone module too


It has to wait before we have gradle-kotlin


Doh, good point Alex :)


I'll try with your code when I'm back home :)

looking forward to contribute


Another question :

I found kool-stream very kool :slight_smile: but have not yet come with a way to integrate it with vert.x. In verti.x there is an EventBus that takes care of asynchronous events. Would you mind giving some comments on this?


:) looking forward to that


I'll try hack up a sample...


I just added a simple example of using Kool Templates with vertx and kotlin. Heres now to run the sample


It seems you forgot to add kotlin.io jar


whoops :) sorry about that - I've added a repository now, so hopefully the mvn build will work now?


I tryed to run the sample, but I get this errors:

ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (29, 30) Unresolved reference: DefaultObjectWrapper ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (40, 37) Unresolved reference: Template ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (30, 42) Unresolved reference: TemplateModel ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (19, 8) Unresolved reference: freemarker ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (20, 8) Unresolved reference: freemarker ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (23, 8) Unresolved reference: freemarker ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (30, 12) 'wrap' overrides nothing ERROR: C:projectsvertex-kotlinsrcorgvertxkotlinfreemarkerJsonWrapper.kt: (31, 15) Unresolved reference: DefaultObjectWrapper

I'm using Apache Maven 3.0.3. I just pulled from github and run

mvn test-compile exec:java

It appear that is lacking a dependency on freemarker , or   am I doing something wrong?


Please use IntelliJ project. Or try to tweak maven file to add freemarker dependency (sorry, I am maven-virgin)


I've just fixed the pom.xml; do a git pull and the build should work again


whoops, forgot to push. OK the pom.xml fix is definitely pushed now :)