Web Application Architecture in Clojure – Getting Started
Usman Aziz is a technical lead at TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains.
After a Clojure development sprint, the next step is to deploy the web application and make it useful for clients, or to conduct some testing. As with Java, the web applications written with Clojure are usually deployed as Web Servlets. The servlets interact with web clients using a request and response model managed by a servlet container. By design, the Clojure web applications support all of the capabilities and interfaces as Java counterparts, making it easier to deploy them on a Java application server with possible support of multi-tenancy. In practice, multiple applications developed using Clojure and Java can be deployed and made to interact on a single application server. Read more about Java and Clojure Web Architecture in Clojure Programming, where this figure comes from:
To avoid some complexities in the servlet API, you should consider using Clojure’s web frameworks, such as Ring, Compujure, Webjure and others. Ring, for instance, uses Clojure’s data structures to define a standard data schema for representing requests and responses. Each Ring handler function takes a translated version of a servlet’s request object and returns a response that the Ring’s adapter translates and writes to the servlet’s response object. Later, these handlers with a pair (route) of HTTP request methods and URL matchers, yield a single function that provide complete functionality of the application for all supported URLs and HTTP verbs. Most developers gain expertise in one or more of these web frameworks, providing for robust application deployment. Read The Foundation: Ring in Clojure Programming to better understand the Ring SPEC, which will help you further understand the Clojure web application architecture.
Packaging & Deployment Steps
Similar to its cousins in Java, a Clojure web application is packaged in a .war file. The .war file generally includes HTML files, and images or media that work with the application. Additionally, there is a WEB-INF directory containing an XML description file, web.xml, that explains the deployment of this application on an application server. This is a lib directory containing numerous .jar files that signify multiple web applications and their dependencies, along with a classes directory that contains the JVM class files as well as Clojure source files. There are many ways to create these .war files. For instance, lein-ring is a plug-in that comes with Leiningen and has many uses besides the production of .war files from the project.clj file of your Clojure project. You can download and learn about lein-ring from their repository. For testing the web application from leiningen, it is recommended you use Jetty server with all requests directed to the root handler provided with the project, using the following command:
lein ring server
The process of deploying a production Clojure application to an application server is similar to the process used for Java applications. It starts with setting up and configuring the application server, followed by copying the .war file to the server, and then restarting the server to load the new files. These simple steps make it very easy to deploy and later update the Clojure applications on a custom application server. There are other tools that are customized for special application servers, such as the Amazon’s Elastic Beanstalk, which is a platform as a service that automates the deployment of applications on Amazon’s web services. In the case of deployment to Amazon’s Elastic Beanstalk, environ can be used, which is a library to manage environment settings. Once a project is set, the following command will deploy the Clojure application:
lein beanstalk deploy production
This single command will perform all of the activities of deploying a web application, starting with building a .war file and copying it to Amazon’s S3 cloud. Later, it will create an Elastic BeanStalk application, with the same name as your project, followed by creating an environment production and deploying the .war file into this environment. It is possible to create as many environments as needed. To check the current version, as well as the selected environment, use the following command:
lein beanstalk info
You should now have a good understanding of Clojure’s web application architecture.
Safari Books Online has the content you need
Check out these Clojure and Java books available from Safari Books Online:
|The Joy of Clojure goes beyond the syntax, and shows how to write fluent, idiomatic Clojure code. Readers will learn to approach programming challenges from a Functional perspective and master the Lisp techniques that make Clojure so elegant and efficient. This book will help readers think about problems the “Clojure way,” and recognize when they simply need to change the way they program. Not just another book about programming philosophy, this book tackles hard software areas like concurrency, interoperability, performance, and more.|
|Clojure Data Analysis Cookbook presents recipes for every stage of the data analysis process. Whether scraping data off a web page, performing data mining, or creating graphs for the web, this book has something for the task at hand. You’ll learn how to acquire data, clean it up, and transform it into useful graphs that can then be analyzed and published to the Internet. Coverage includes advanced topics like processing data concurrently, applying powerful statistical techniques like Bayesian modelling, and even data mining algorithms such as K-means clustering, neural networks, and association rules.|
|Clojure Programming, helps you learn the fundamentals of Clojure with examples relating it to the languages you know already—whether you’re focused on data modeling, concurrency and parallelism, web programming, statistics and data analysis, and more.|
|The Java® Tutorial: A Short Course on the Basics, Fifth Edition, is based upon Release 7 of the Java Platform Standard Edition. This revised and updated edition introduces the new features added to the platform, including a section on NIO.2, the new file I/O API, and information on migrating legacy code to the new API. Information reflecting Project Coin developments, including the new try-with-resources statement, the ability to catch more than one type of exception with a single exception handler, and support for binary literals and diamond syntax, which results in cleaner generics code, has been added where appropriate.|
About the author
|Usman Aziz is a technical lead at TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains. He holds a degree in Computer Systems Engineering. His current focus is on protecting bulk data. He can be reached at email@example.com.|