JVM Rocks

java, groovy, grails, gradle, clojure.

Vertx: Create Vertx Instance in Cluster Mode (Clojure)

Setup project as described in previous post

open a terminal window, run lein repl

1
lein repl

require the following namespaces

1
2
3
(require '[vertx.embed :as embed]
       '[vertx.eventbus :as eb]
       '[vertx.http :as http])

set the following system property

1
2
(System/setProperty "vertx.clusterManagerFactory"
                   "org.vertx.java.spi.cluster.impl.hazelcast.HazelcastClusterManagerFactory")

set the vertx instance

1
 (embed/set-vertx! (embed/vertx "localhost"))

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")))

Vertx: Create Vertx Instance(clojure)

Setup project as described in previous post

open a terminal window, run lein repl

1
lein repl

require vertx.embed and then set vertx instance atom

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

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>Vertx Rocks!</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")))

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")))

Clojure: List Public Methods in a Java Class

use the function below to list the public methods in a java class

list-methods function
1
2
3
4
5
(defn list-methods
  "List methods in a class"
  [c]
  (clojure.pprint/pprint
    (map #(.toString %) (.getMethods c))))

or using the threading macro

list-methods function : using threading macro
1
2
3
4
5
6
7
(defn list-methods
  "List methods in a class"
  [c]
  (->> c
    (.getMethods)
    (map #(.toString %))
    clojure.pprint/pprint))

examples

examples
1
2
3
4
5
;; methods in the String class
(list-methods String)

;; public methods in the Long class
(list-methods Long)

Gradle: Filter a File Using Ant Filters

If you want to replace tokens in a file with values from a properties file

For example your file (profile.txt) contains the entry

1
name=@name@

and your properties file (values.properties) contains an entry

1
name=Ted Jurey
1
2
3
4
5
6
7
8
9
10
11
12
import org.apache.tools.ant.filter.ReplaceTokens

task filter(type: Copy) {
  from 'profile.txt'
  into 'filtered'
  def propreties = new Properties()
  file('values.properties').withInputStream {
      properties.load(it)
  }

  filter(ReplaceTokens, tokens: properties)
}

Gradle: Extract a Folder From a Zip File

Let’s say you have a zip file with the containing these two folders(lib, classes) and you want to extract the lib folder to another zip file.

home.zip
-lib
-classes

first extract the zip contents into a folder

1
2
3
4
task extractZip(type: Copy) {
  from zipTree('home.zip')
  destinationDir = file('temp')
}  

then use another task to create the desired zip file

1
2
3
4
5
task createZip(type: Zip, dependsOn: extractZip) {
  baseName = 'lib'
  from ('temp/home') { include 'lib/**'}
  destinationDir = file('dist')
}