Getting Started: Deploying a Web Application

Welcome back! This article covers creating a basic Ring web application and deploying to an Immutant. It is the second installment in a series of tutorials on getting started with Immutant. If you haven't read the first installment, go do so now, since it covers installation and setup. This tutorial assumes you are on a *nix system.

Creating an Immutant Clojure application

In our previous article, we installed the lein plugin. Let's take another look at the tasks it provides:

~/immutant $ lein immutant
Manage the deployment lifecycle of an Immutant application.

Subtasks available:
new        Create a new project skeleton initialized for immutant.
init       Adds a sample immutant.clj configuration file to an existing project
deploy     Deploys the current project to the Immutant specified by $IMMUTANT_HOME
undeploy   Undeploys the current project from the Immutant specified by $IMMUTANT_HOME
run        Starts up the Immutant specified by $IMMUTANT_HOME, displaying its console output

We talked about run last time. This time, we'll cover new and deploy. To do so, we'll build a basic application that demonstrates the current web features. To get started, let's create an Immutant project:

~/immutant $ lein immutant new immutant-demo
Created new project in: /Users/tobias/immutant/immutant-demo
Look over project.clj and start coding in immutant_demo/core.clj
Wrote sample immutant.clj

The new task creates a Leiningen project and gives it a sample Immutant configuration file (immutant.clj). It is equivalent to calling:

~/immutant $ lein new immutant-demo && cd immutant-demo && lein immutant init

We'll come back to immutant.clj in a sec. Now, let's add a ring handler to our core namespace:

(ns immutant-demo.core)

(defn ring-handler [request]
  {:status 200
    :headers {"Content-Type" "text/html"}
    :body "Hello from Immutant!" })

Configuring the application for Immutant

When the Immutant deploys an application, it looks for a file named immutant.clj at the root and evaluates it if it exists. This file is used to configure the Immutant services you want your application to consume. It's the single place you defines all the components required by your application, and saves you from having to keep external configuration files in sync (crontabs, message queue definitions, init scripts, etc).

The file has example code for configuring web endpoints and messaging services, but we're just going to deal with web endpoints in this article. Edit your immutant.clj so it looks like:

(ns immutant-demo.init
  (:use immutant-demo.core)
  (:require [immutant.messaging :as messaging]
            [immutant.web :as web]))

(web/start "/" #'ring-handler)

We'll come back to what web/start is doing after we get the application running.

Deploying your application

Before we can start up an Immutant, we need to tell it about our application. We do that by deploying (for this to work, you need to have IMMUTANT_HOME set - see the previous article for details):

~/immutant/immutant-demo $ lein immutant deploy
Deployed immutant-demo to /Users/tobias/immutant/current/jboss/standalone/deployments/immutant-demo.clj

This writes a deployment descriptor to Immutant's deploy directory which points back to the application's root directory. Now the Immutant can find your application - so let's fire it up.

Starting Immutant

To launch an Immutant, use the lein immutant run command. This will start the Immutant's JBoss server, and will run in the foreground displaying the console log. You'll see lots of log messages that you can ignore - the one to look for should be the last message, and should tell you the app was deployed:

~/immutant/immutant-demo $ lein immutant run
Starting Immutant via /Users/tobias/immutant/current/jboss/bin/standalone.sh
...
(a plethora of log messages deleted)
...
13:04:39,888 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "immutant-demo.clj"

Now, let's verify that our app is really there. Immutant runs on port 8080 by default, so let's hit it and see what happens:

~ $ curl http://localhost:8080/immutant-demo/
Hello from Immutant!

Yay!

You can kill the Immutant with Ctrl-C.

Context Paths

Remember our call to web/start earlier? Let's talk about what that is doing. To do that, however, we need to first talk about context paths. The context path is the portion of the URL between the hostname and the routes within the application. It basically tells Immutant which requests to route to a particular application.

An Immutant can host multiple applications at the same time, but each application must have a unique context path. If no context path is provided when an application is deployed, it defaults to one based on the name of the deployment. The deployment name is taken from the name of the deployment descriptor, which in turn is taken from the name of the project given to defproject in project.clj. So for our sample app above, the context path defaults to /immutant-demo. You can override this default by specifying a :context-path within an :immutant map in your project.clj. Let's go ahead and do that:

(defproject immutant-demo "1.0.0-SNAPSHOT"
  :description "A basic demo"
  :dependencies [[org.clojure/clojure "1.2.1"]]
  :immutant {:context-path "/"})

Now, when you call lein immutant deploy, the context path will be picked up from your project.clj and included in the deployment descriptor and any web endpoints your application stands up will be accessible under that context path.

Which brings us back to web/start. web/start stands up a web endpoint for you, and takes two arguments: a sub-context path and a Ring handler function. The sub-context path is relative to the application's context path, so a context path of "/ham" and a sub-context path of "/" makes the handler function available at /ham, whereas a sub-context path of "/biscuits" makes the handler function available at /ham/biscuits. Make sense?

You can register as many web endpoints as you like within an application - they just each need an application unique sub-context path. If we add this to our core.clj:

(defn another-ring-handler [request]
  {:status 200
   :headers {"Content-Type" "text/html"}
   :body "Pssst! Over here!"})

And this to our immutant.clj:

(web/start "/biscuits" #'another-ring-handler)

Redeploy the application to pick up the :context-path from immutant.clj:

~/immutant/immutant-demo $ lein immutant deploy
Deployed immutant-demo to /Users/tobias/immutant/current/jboss/standalone/deployments/immutant-demo.clj

Then fire an Immutant up again with lein immutant run, we can see they both work:

~ $ curl http://localhost:8080
Hello from Immutant!
~ $ curl http://localhost:8080/biscuits
Pssst! Over here!

web/start has a companion function for shutting down a web endpoint: web/stop. It takes the sub-context path for the endpoint, and can be called from anywhere. You aren't required to shut down your endpoints - Immutant will do that on your behalf when it is shut down or the application is undeployed.

Wrapping up

I hope you've enjoyed this quick run-through of deploying a web application to Immutant. Since Immutant is still in a pre-alpha state, none of what I said above is set in stone. If anything does change, I'll edit this post to keep it accurate. I've posted the demo application we've built if you want to download it.

If you have any feedback or questions, get in touch! And stay tuned - our next tutorial will cover using Immutant's messaging features.

Getting Started: Installing Immutant

This article is out of date - see our new tutorial for the latest instructions for installing Immutant.

Greetings! This article is the first in a series of tutorials on getting started with Immutant. This entry covers setting up a development environment and installing Immutant. This tutorial assumes you are on a *nix system. It also assumes you have Leiningen installed. If not, follow these instructions, then come back here.

Installing the lein plugin

We provide a lein plugin for creating your Immutant applications and managing their life-cycles. As of this post, the latest version of the plugin is 0.2.0. Check clojars for the current version.

Let's install it as a global plugin:

~ $ lein plugin install lein-immutant 0.2.0
Copying 3 files to /a/nice/long/tmp/path/lib
Including lein-immutant-0.2.0.jar
Including clojure-1.2.0.jar
Including commons-exec-1.1.jar
Including fleet-0.9.5.jar
Created lein-immutant-0.2.0.jar

Now, run lein immutant to see what tasks the plugin provides:

~ $ lein immutant
Manage the deployment lifecycle of an Immutant application.

Subtasks available:
new        Create a new project skeleton initialized for immutant.
init       Adds a sample immutant.clj configuration file to an existing project
deploy     Deploys the current project to the Immutant specified by $IMMUTANT_HOME
undeploy   Undeploys the current project from the Immutant specified by $IMMUTANT_HOME
run        Starts up the Immutant specified by $IMMUTANT_HOME, displaying its console output

We'll only talk about the run task today, and save the rest for the next tutorial.

Installing Immutant

Now we need to install an Immutant distribution. We've yet to make any official releases, but our CI server is setup to publish an incremental build every time we push to the git repo. The latest incremental build is always available at http://immutant.org/builds/immutant-dist-bin.zip.

At this point, I'd love to tell you to install Immutant via:

lein immutant install # coming soon!

But that currently won't work. Until we actually implement that, you'll have to download and setup Immutant manually. Go ahead and download our latest incremental build.

Once you have the zip file downloaded, unzip it somewhere handy. For this walk-through, I'm going to put it in ~/immutant/. When unzipped, the distribution will have the build number in the directory name. It's handy to have a consistent path to the distribution, allowing us to download other incremental builds in the future with minimal fuss. To do that, link the distribution to current. Here is my shell session doing just that:

~ $ mkdir immutant
~ $ cd immutant
~/immutant $ unzip -q ../downloads/immutant-dist-bin.zip
~/immutant $ ln -s immutant-1.x.incremental.15 current
~/immutant $ ls
current                     immutant-1.x.incremental.15
~/immutant $ ls -l
total 8
lrwxr-xr-x  1 tobias  staff   27 Nov  7 22:41 current -> immutant-1.x.incremental.15
drwxrwxr-x  3 tobias  staff  102 Nov  7 13:20 immutant-1.x.incremental.15

Now we'll set a environment variable pointing to the distribution:

export IMMUTANT_HOME=~/immutant/current

This will tell the lein plugin where to find Immutant. Once we implement lein immutant install, this requirement will go away.

Running Immutant

To verify that Immutant is properly installed, let's fire it up. To do so, use the lein immutant run command. This is a convenient way to start the Immutant's JBoss server, and will run in the foreground displaying the console log. You'll see lots of log messages that you can ignore - the one to look for should be the last message, and will tell you the Immutant was properly started:

~/immutant $ lein immutant run
Starting Immutant via /Users/tobias/immutant/current/jboss/bin/standalone.sh
...
(a plethora of log messages deleted)
...
22:46:31,663 INFO  [org.jboss.as] (Controller Boot Thread) JBoss AS 7.x.incremental.182 "Ahoy!" started in 2990ms - Started 136 of 200 services (61 services are passive or on-demand)

You can kill the Immutant with Ctrl-C.

Wrapping up

If you've done all of the above, you're now ready to deploy an application. We'll cover that in our next tutorial.

Since Immutant is still in a pre-alpha state, none of what I said above is set in stone. If anything does change, I'll edit this post to keep it accurate.

If you have any feedback or questions, get in touch!

WTF is an enterprise-grade app server?

That's a great question. We get it a lot. It was asked on Hacker News in response to our birth announcement of Immutant yesterday.

So as someone who has worked in multiple enterprises, and now works for a company whose primary customers are enterprises, and since I routinely toss around the term like everyone knows what it means, and since it's one of those awful terms that means something different to everyone...

I'm compelled to answer the question, "WTF is an enterprise-grade application server?"

The answer requires answering another question first:

What's an Enterprise?

Here's my definition: it's an organization of mostly-independent teams building and maintaining applications used by other internal groups and external customers.

The key identifier of an enterprise is "a group of groups" in which more than one of them builds software.

There's usually a single "system operations" group. Their life is hell. They're ultimately responsibile for the security and integrity of the organization's data and the lifecycles of all the applications built for and used by all the other groups in the organization.

Did I mention their life is hell?

They can't afford to support all the myriad databases, message queues and web frameworks each team might decide to build their apps around. Not to mention supporting multiple languages! They prefer a single, "all in the tin" solution. Sadly, that usually means .Net or Java.

So enterprise-grade implies a capacity for supporting these types of environments. It usually involves, among other things, messaging so that the disparate apps may communicate, transactions to ensure the integrity of distributed data stores, and clustering, not only for scalability, but also to allow the lifecycles of the multiple versions of the apps to vary independently. And oh yeah, it also has to stay up, all the time.

What's an App Server?

An app server is a single product that provides all those services. It's a multi-threaded process that, once started, provides any app deployed to it with a web server, messaging, transactions, scheduling, security, caching, clustering, and more. JBoss AS7 is one such app server. It's open-source and it's fast.

Unfortunately, most popular commercial Java app servers provide those robust enterprise services at a very high price. Not only in dollars, but in the form of complex Java API's, overly-configured slow implementations, vendor-negotiated standards and general "acronym soup".

But it doesn't have to be that way. The Rails "convention over configuration" mantra inspired the creation of TorqueBox, encapsulating the enterprise-grade services provided by JBoss AS7 behind simple Ruby API's. The immediate goal for Immutant is to do the same with Clojure.

But by integrating any JVM-based language with JBoss AS7, the ultimate goal is to keep all the groups in an enterprise -- both operations and development -- happy. Or at least significantly less soul-sucky and hopefully more productive.

But why should you care?

Maybe you shouldn't. Maybe you only need a web server and a database, maybe just a JVM! Maybe you're not in a group of groups. Heck, maybe you're not even in a group!

Maybe you're perfectly content being both developer and admin for your apps and the various external processes on which they depend, or you're quite happy delegating some of those responsibilities to the fine folks at Heroku, EngineYard, or someone else.

If that's the case, you are probably a very happy person, and I'm very happy for you!

But if you feel you could benefit from a more integrated "all in the tin" solution, and especially if you're thinking along polyglot lines, I think TorqueBox (and Immutant, once it matures) is a compelling alternative, whether you work in an enterprise or not.

Introducing Immutant

[Clojure]

The time has come for us to officially announce the Immutant project. Immutant is going to be for Clojure what TorqueBox is for Ruby: a true, enterprise-grade application server, encapsulating the robust services provided by JBoss AS7 within intuitive Clojure API's.

Further, Immutant has inspired us to attempt to achieve a sort of "polyglot cafeteria plan" of app serving. For example, your "application" might consist of a Ruby web app in the front and Clojure data mining services in the rear, loosely but securely coupled via JSON-encoded asynchronous messages. You choose the JVM-based languages and frameworks that make the most sense for each of your application components. And Immutant will provide a uniform deployment platform and the "glue", i.e. those critical enterprisey services like messaging, security, transactions, clustering, etc. that all successful applications eventually require.

Why?

Because learning new programming languages is fun. Because no one language is good for everything a non-trivial application needs to do. Because JBoss wants to be "everywhere".

But mostly because we want to.

App servers get a bad rap -- and bad app servers deserve it -- but simple deployment, clustering and messaging are all good things. Embracing polyglot and enabling the choice of the right language/framework for a specific job is a good thing. We hope to deliver that goodness via Immutant.

An Invitation

We just got started with this, and though still Clojure noobs, we're learning fast. We have a lot to do, and we'd love to have you help shape Immutant into something that'll best serve your needs.

In the coming weeks, we'll be publishing more articles about Immutant, such as:

  • How to deploy your Clojure apps (hint: Leiningen plugin)
  • How to configure Immutant to serve both Ruby and Clojure apps
  • How to pass messages between Ruby and Clojure components
  • How to distribute Clojure message handlers across a cluster of immutants

Please follow along on Twitter, join the mailing lists and/or hang out with us on IRC. It'll be fun!