Clojure Mount

Once you learn Clojure (or any programming language) well enough to tackle a large project, you are going to run into lifecycle, dependency, and state management issues.

Two great resources for handling lifecycles, dependency, and state in Clojure apps are Mount and Component. I use Mount for my projects, but both are popular solutions. This post is about tolitius/Mount.

Simple Mount Example

Continue reading “Clojure Mount”

Java Servlets Written in Clojure

Yes, you can write Java Servlets in Clojure.

Create a Clojure Servlet project with the following command.

The important parts of the command are ‘luminus’, which is the framework used to create the Servlet, and ‘+war’, which adds uberwar and Servlet routes to the project. This creates a file called ‘handler.clj’ that replaces the traditional Java Servlet file. The configurations for the Clojure Servlet are found in ‘project.clj’ in the :uberwar  section of the configurations.

By default the lein command above creates a project that will build a WAR file called my-clj-servlet.war, and when deployed will deploy in the my-clj-servlet context, meaning on a default installation of Tomcat you would find the link at http://localhost:8080/my-clj-servlet/

It is important to note that the resulting project from the above lein command actually won’t work as a Servlet, yet. But, we’ll fix that below. The project is configured in the hopes that you can run it from a Servlet container or as a standalone app, and with some massaging, you could. However, this tutorial only focuses on how to get the project compiling as a Java Servlet written in Clojure. If there is enough interest, I’ll write another tutorial on how to get the default project working as a standalone app, and a Servlet.

Continue reading “Java Servlets Written in Clojure”

HTML Email from Clojure

As mentioned in my other post about plaintext emails, I recommend using the Apache Commons Email library when sending email from Clojure apps.  First up, you need to add the dependency to ‘project.clj’

Next, your HTML email has both an HTML version and the alternative plaintext version of the email. Notice, all you have to do is create an instance of HtmlEmail and then call the set and send methods on it using ‘doto’. Pretty easy.

There are more examples and the javadocs over at the Apache Commons site. However, this should be a basic enough example to get you started.

 

 

 

Plain Text Email from Clojure

I recommend using the Apache Commons Email library when sending email from Clojure apps. Here’s a simple example.

First up, you need to add the dependency to ‘project.clj’

Next, your code would look something like the following. Caution, … This will send emails, so configure it to send the emails to a personal email address for testing purposes.

 

Clojure UTC DateTime

When saving datetimes to databases like MySQL, it is best to save them as UTC instead of using your local timezone. That way, if your company, or IT department, or servers move to a new timezone, you won’t have to worry about adjusting every time stored in the database to match the new timezone. The trick is that you always return UTC times in your APIs, and then expect the clients or tools using your APIs to convert the provided UTC time to their local timezone for display.

This leads to a common question. How do I get the current datetime in UTC when using Clojure?

The formatted string with the UTC time will be yyyy-MM-dd hh:mm:ss.

Clojure Migratus: “Migration reserved by another instance”

Migratus is a great migration tool for Clojure enterprise projects. Migrations allow you to do and undo changes to a database very quickly. The best use case for Migratus is update of a database for your enterprise project or setting up a new database for a development environment. Setting up the database can be on a production server or your local development environment.

Eventually one of your migrations is going to have an SQL exception. Normally, SQL exceptions aren’t a big deal. You just fix it, and run the migration again. However, Migration gets into an :ignore state and gives you an exception reading …

… and then refuses to run another migration no matter what you do.

The fix is to go into your ‘schema_migrations’ table in your database and remove the row that has an id of -1.

That’s really a simple fix for your favorite Clojure migration tool.

 

Structure and Interpretation of Computer Programs

The book, Structure and Interpretation of Computer Programs, is considered one of the top necessary reads for software engineers. It is a fairly expensive book to buy hard copies of. Luckily, the authors generously provide a PDF version for free. Currently, you can download the free version from the sicp-pdf on GitHub. Also, MIT offers a free download at https://web.mit.edu/alexmv/6.037/sicp.pdf.

Structure and Interpretation of Computer Programs is written in Scheme, but is all applicable to Clojure. Functional programming is taught thoroughly, along with lazy sequences and higher order functions.

Structure and Interpretation of Computer Programs doesn’t ignore the other side of the fence. It also goes over objects, state, and mutable data.

This is a Computer Science book, so expect some deep thinking and descriptions. But, if you’re looking to improve your software engineering skills, give reading this book a go.

Private Functions in Clojure

Clojure developers coming from Object Oriented backgrounds know how useful it is to separate implementation details from functions and methods that are supposed to be used by other coders. Java has public and private methods. This allows devs to limit usage and visibility of functions that are not meant to be used directly.

There are two options in Clojure for keeping functions private. First, you can use a special macro, defn- . defn-  works just like defn , except that the function is only visible inside the current namespace. This makes the function private in the Java sense to the term. Continue reading “Private Functions in Clojure”