JVM Rocks

java, groovy, grails, gradle, clojure.

Vertx: Create Vertx Instance Using Platform Manager (Clojure)

Setup project as described in previous post

open a terminal window, run lein repl

1
lein repl

import the platform locator

1
(import 'org.vertx.java.platform.PlatformLocator)

create instance of platform manager

1
(def pm (.createPlatformManager PlatformLocator/factory))

require vertx.embed and then set vertx instance atom

1
2
(require '[vertx.embed :as embed])
(embed/set-vertx! (.vertx pm))

now you can start a server, for example to start a http server do the following

import the http namespace

1
  (require '[vertx.http :as http]))

declare a request handler

1
2
3
4
  (defn req-handler [req]
  (-> (http/server-response req)
      (http/add-header "Content-Type" "text/html; charset=UTF-8")
      (http/end "<html><body><h4>It's working!</h4></body></html>")))

start server using the handler and assign it to a variable server

1
2
3
(def server (-> (http/server)
    (http/on-request req-handler)
    (http/listen 8080 "localhost")))