Tuesday, October 4, 2011

JavaOne 2011: Clojure in the Enterprise: Easy Concurrency with Java Integration

I had to really move to get from Parc 55 Embarcadero (for the JavaOne Groovy 1.8 session) to Moscone South for the Oracle OpenWorld Java 12 Roadmap presentation (I was a couple minutes late). I had 45 minutes to make my way back from Moscone South to the very same Parc 55 Embarcadero for the JavaOne presentation "Clojure in the Enterprise: Easy Concurrency with Java Integration" (25165). This presentation was offered by Salesforce.com's Pat Patterson and Heroku's Mark McGranaghan.

After each speaker introduced himself, Patterson introduced Clojure and its brief (since 2007) history. He noted that Clojure was built on the JVM to leverage strengths most of us are familiar with and that are the reasons numerous languages have been based on the JVM. He talked about performance, libraries, and the wealth of investment in the JVM being leveraged by Clojure.

Patterson introduced some of the benefits of functional development by first talking about Lisp and then moving into Clojure's Lisp heritage. He talked about Clojure's favoring of immutability over mutual state and the advantages of this in concurrent environments. His simple slide for this part of the presentation listed four words: expressiveness, correctness, robustness, and dynamism.

Patterson called McGranaghan the "Clojure expert in the room" and turned the syntax slides over to him. McGranaghan covered a slide with Clojure syntax compared to Java syntax for a public method named "hi" with a void return type. Another slide showed Java nad Clojure treatment of getting current time in milliseconds since the epoch time. The intent of that slide was to show that Clojure allows direct access to Java.

McGranaghan introduced REPL (read about print loop) and showed how they makes it easy to interactively use Clojure code. He then showed using Clojure to make new, temporary copies of a list without modifying the original list.

Patterson showed a more sophisticated example and showed how to convert a Map to a parallel Map with the simple addition of a letter "P". He stated that it is incredibly easy to build high performance applications in Clojure for applications that are heavily I/O bound with no dependencies between each access.

Like most evangelists, these speakers believe that Clojure works well just about anywhere, but they did state that it is a particularly good fit for "high performance web sites," "parallel data processing," "rigorous business logic," and "concurrent programming." Because of its Lisp and JVM traits, most of these are not surprising areas where it could be strong.

McGranaghan stated that Heroku is one easy way to get a Clojure application into production. He talked about it being difficult to deploy to the cloud generally, but that a cloud framework makes it easier.

Patterson looked at Clojure in a "Hello World" web example. His example used Jetty and Ring. Like several of the previous examples, Patterson also used leiningen. Unfortunately, the example, obviously knowing it was being demonstrated, decided not to work. (I've been there as a presenter and it's not fun). Patterson would later explain that this was a case of being certain a small change would have no impact and making that change without testing it (I'd like to say I've never done that, but I better just move on).

McGranaghan talked about using Clojure with PostgreSQL. He specifically showed Clojure syntax for creating a table, inserting a record, and querying the results. He referenced a demonstration called SHOUTER that demonstrates the database access. As part of his local use of SHOUTER, McGranaghan talked about Heroku's procfile and Foreman concepts. Patterson reiterated Clojure's built-in PostgreSQL support.

It was interesting that in the Q&A section, the speakers pointed out that the reason Clojure can get away with so many immutable objects and the need to create new objects that quickly become garbage is the tremendous gains in JVM optimizations. This reiterates the value of building the Clojure language on the JVM. They also explained that it is not complete duplication of objects. The example was given of a Java Map with one name/value added. The entire Map is not copied in such cases.

One of the things this presentation provided for me was a realization of how many Clojure tools and frameworks are already available. The presentation was as much about cloud deployment and Heroku as it was about Clojure or concurrent programming. If that was not enough, a lesson on "the importance of testing" was thrown in as well. The book The Joy of Clojure was referenced in this book for developers wishing to go beyond "Hello World" examples in clojure. Other Clojure books include Clojure in Action (MEAP), Practical Clojure, and Programming Clojure.