Porting the Netty DiscardServer example to clojure

I recently looked at the Netty library as the basis of a server for work, so after a recent LambdaJam I was inspired to write their DiscardServer example in Clojure, on a basic, line-to-line basis. Has some issues, not the least of which is that at the time of writing their example is missing sections, and doesn’t compile. Was able to guess my way around enough of it to get it working in Java first:

Initial attempts writing the Clojure had some problems. There was some confusion due to a bug in clojure which didn’t correctly call the public, final method of a non-public, abstract superclass. This meant that the call to ServerBootstrap.channel() failed, an issue that I worked around by writing a static Java method to call the original method:

Forgot about Clojure/java varargs calls, and the way that Java’s variable argument methods actually used an array under the hood. This confused me for a while, as my initial attempt to call ChannelPipeline.addLast(ChannelHandler…handlers) resulted in ClassCastException:

One workaround is to create the array with a single value:

but this seemed fairly clumsy, exposing some messy implementation details to get things working. Still not a great solution, I instead decided to write another Java method to call the original:

This make the Clojure call a little clearer, though I can go back to the original array creation if I do need more than one handler:

End result, the Netty java class for working around interop issues:

and the Clojure:

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *