Daemonize your Java application with the “Fat Controller”

The Fat Controller is a parallel execution handler that repeatedly runs other programs, a bit like cron and Apache Daemon. It is simple to use yet has some nice features that makes it a great tool for simple and complex background processing tasks. The software is Open Source and licensed under GNU GPL v3.

Let’s begin by discussing what a daemon is. A daemon is a process which is not connected to a terminal or user interface, instead running in the background. Daemons are often used by operating systems for long-running tasks and services, such as handling network connections or hardware monitoring. Web servers generally run as daemons so that they are not tied to a particular user’s login session which allows other users to also control the application.

It is sometimes useful to be able to create your own daemons to run continually in the background and perform a certain task, and this is not uncommon when developing web applications where daemons are often used for processing queues or monitoring performance. Making a program run as a daemon usually involves just adding a few blocks of boiler-plate code, but as Java applications do not run natively but rather inside the Java Virtual Machine, there is no way to make a program daemonise itself, by which I mean detach itself from the session which started it and run in the background. Instead, the JVM has to be made to run in the background which is possible if wrapped with a few lines of shell script.

You might now be wondering what the issue is: most languages provide support for applications to daemonise themselves and there is a workaround for Java. The problem is that in each case, in order to make a program run as a daemon involves adding code. Not only does this pollute your business logic with runtime handling, but also adds risk. It would be far better to have one standard solution which can daemonise anything, and that is exactly what I have been developing recently and would like to present in this article.

The Fat Controller

The Fat Controller takes its name from the boss of the engines in the Thomas the Tank Engine stories and began life as a simple program able to daemonise itself and run any other program inside itself. It is configurable using a simple config file and can be started and stopped in the familiar Linux way:

⁄etc⁄init.d⁄fatcontrollerd start
⁄etc⁄init.d⁄fatcontrollerd stop

Cron vs. The Fat Controller

Cron runs programs at specified times or with a specified frequency, for example every minute or every hour. This ability to run a program with a regular frequency can be very useful, let’s take an example of a web application which needs to keep a backup of all files uploaded by users. Rather than doing one big backup at the end of each day, it’s much better to make smaller yet regular backups throughout the day. For this example, let’s say the backup script is run by Cron once every 10 minutes and copies all new files to the backup storage. Now let’s assume that the website suddenly gets a lot of traffic and a lot of people upload a lot of new files. The backup script then has to copy all these new files which takes more than 10 minutes. However, Cron doesn’t care about this and runs the script again after ten minutes, now we have two backup scripts running at the same time; which depending on the implementation, may cause problems.

The Fat Controller, on the other hand, can be configured to run programs with a specified wait time between one instance ending and the next starting, see the diagram below:

Cron vs The Fat Controller: Whereas in Cron overlaps can occur and not be prevented, the Fat Controller allows to specify a wait time between two runs.

Of course this issue of overlapping instances with Cron can be overcome with a locking strategy, but why complicate your business logic and increase risk? It’s far better to use a standard and proven solution.

Is it reliable?

The Fat Controller is written in C for stability and the ability to completely isolate itself from whatever program it might be running. If the program it is running crashes, the Fat Controller can handle this and be configured to take various courses of action, such as trying to restart the crashed program.

Can I use it in production?

Absolutely. The Fat Controller is already running on many servers around the world and has been known to run for months on end. In over two years since its first release there hasn’t been a single crash report.

Where can I get more information?

The Fat Controller has many more strategies for limiting multiple processes. It even has various strategies for allowing and controlling multiple instances so you can parallelise anything. All of this is described in use-cases along with comprehensive documentation on the website:

http://fat-controller.sourceforge.net/

The Fat Controller can function as a simple version of Apache Daemon but without the need to implement a special API in Java. So, if you don’t need the extra complexity afforded by Apache Daemon then perhaps consider The Fat Controller.

Share

Leave a Reply

*