Using ZeroMQ from Clojure

Published January 26, 2013

I'm quite the JVM noob with only 6 months of Clojure experience, and no prior JVM experience. After some hacking and tweaking, I managed to figure out how to use ZeroMQ from Clojure via Leiningen (the build tool). ZeroMQ is a native library that is packaged in a Java API via JNI, so the steps required weren't obvious to me.

These steps are slightly opinionated, but they should be easily modifiable if your opinion differs from mine.

Step 0: Install ZeroMQ

Do it! My development box is currently a Mac running OS X, so I used Homebrew. Your package manager of choice probably has a package for ZeroMQ you can use.

Step 3: Set up Leiningen

As you can see, we assume you put JZMQ into the same folder as your leiningen project, by referring to it via "../". This has two benefits:

You don't hardcode the absolute path into your project.clj. The actual path of a default JZMQ install will differ from system to sytem. Some might have it in /usr/local, some in $HOME/.brew/whatever, and so on. With a relative setup, you don't enforce any specific directory structure other than requiring that zeromq is in the same folder as your Leiningen project.

You get the specific version of JZMQ your project requires. If you use an absolute path, say /usr/local, you risk getting a different version of JZMQ than you intended, since you might have installed it into /usr/local via a package manager that might or might not install the version you expect it to.

Step 4: Use it!

Most Clojure wrappers around Java APIs are just, well, Clojure wrappers, so I prefer to use the Java APIS directly in almost all cases, unless the Clojure wrapper adds some value beyond just wrapping. Java interop is really easy with Clojure though, I recommend trying it out.