Skip navigation

Category Archives: Spring Framework

One of the common things any application needs to do is to schedule some kind of recurring tasks. This can be anything from sending automated email to dumping data to an XML format to processing data. In Java, your choice of tool depends on the task at hand.

The Basics

At the heart of all tasks in Java are the java.util.Timer and java.util.Timer classes. You can instantiate a Timer and schedule a TimerTask to recur on a regular basis. This is pretty straightforward and is an easy way to accomplish a very simple tasks in an isolated piece of code.

See the tutorial here: Scheduling recurring tasks in Java applications.

More Juice

Sometimes you need more juice. Perhaps your tasks occur within the framework of a larger code base. You want it to be integrated with the rest of your business logic, service classes, domain objects and so on. You may also need more advanced features like thread pooling, complex scheduling, and other kinds of resource management.

I’m currently using the Spring Framework which provides a great platform to accomplish complicated tasks. The Spring Core includes classes and mechanisms to handle recurring tasks in the context of an application server.

The TaskExecutor interface has a number of implementations that allow the user to do things like thread concurrency, synchronous execution, and thread pooling. The TaskScheduler interface exposes a number of different ways to schedule a task including cron-style and triggers based on the last execution.

But perhaps the biggest win is that it supports some of the basic mechanisms found in the Spring Framework. It supports declaring scheduled methods by annotation.

Find a tutorial here: Task Scheduling Simplifications in Spring 3.0.

Big Guns

Real enterprise environments require even more power. They process large volumes of data, tasks are mission-critical, business rules are complex, integration is complex and things must happen in a transactional manner.

Spring again has a solution for this. It is called Spring Batch. It supports all the things I’ve mentioned and also has a Spring Batch Admin console to easily monitor and manage your tasks.