Zed Shaw’s Mongrel2 uses zeromq as the communication protocol between the web server and any custom handlers that you wish to write in the language of your choosing, including Clojure via a ring adapter. So off my fingers trotted installing all the dependencies for mongrel2, including zeromq, testing my mongrel2 by walking through the examples in the excellent manual. Now for the Clojure part and disaster! I built the Java JNI code for zeromq using make, I tried to use the Clojure ZMQ libraries but they were all written against an earlier version of the Java JNI code, so I stopped in frustration. It was quite easy to proceed by writing Clojure code directly against the Java JNI code I had compiled, but the control freak in me was thinking what happens if the JNI API changes again. At this point I started investigating JNA, which is meant to be an improvement over JNI – it looked like an improvement to me since it looked like I wouldn’t need to code any C or C++ or use make.



The C API defined by zeromq in zmq.h looked simple, all the functions had man pages and the documentation had a complete set of calling examples in C – so it looked like it would be easy. I started simply, could I call the zmq_version function:

Test that with a unit test:

I ran the unit test in the IDE and success, I ran the unit test from a shell with maven and success, it felt quite strange having to basically allocate my own memory in Java but it worked and after an evenings work I had successfully wrapped most of zmq.h with JNA and had unit tests for most of the calls. Library code shown below:

These additional classes are needed to represent structures in the C library.

An incomplete unit test:

So it worked, and was remarkably painless! In the next month or so I will test it on OS X and Windows, replicate all the examples from the zeromq documentation as unit tests, wrap it with a more Java-friendly API and place it on github, so watch this space.