Friday, May 4, 2012

Spring 3 Task Execution and Scheduling

I have downloaded Spring3 and Quartz and Spring batch Framework.
After running a simple example, It was not easy, not working well with autowired annotation and many things to learn before use giant functions. currently, Spring batch depend on spring 2.5 that I am not sure, it will work well with Spring 3.1
It is also causing to make big packaging jar/war file

I just wanted to make a very simple batch program that I can use based on SpringFramwork which can autowire beans and DB resources.
I decide to use Spring Task. It just need Spring framework, nothing else
It is extreamly simple and works perfect as I expected.

Just type 3 more line on you spring xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:mvc="" xmlns:xsi=""
 xmlns:util="" xmlns:p=""

<context:component-scan base-package="monitor"/>

<mvc:annotation-driven />

<!--  Omit all the WEB MVC Handler and View Resolver Configuration -->

<!--  Scheduler define start -->
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

<task:executor id="myExecutor" pool-size="5"/>

<task:scheduler id="myScheduler" pool-size="10"/>
<!--  Scheduler define End -->


Make one Class that is Component annotated. The Class must be in base-package or sub package.
and make method that is Scheduled annotated with Cron Expression.
That's it. that it will run every 5 minutes from 2 min of the hour.
You can autowire beasn which you like.

package monitor.batch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

public class HarvestHttpStatusJob {

 private final Log logger = LogFactory.getLog(this.getClass());

 //To-do  @Autowired beans as usual you need.

 @Scheduled(cron = "0 2/5 * * * *")
 public void harvestStatus() { + " Start");

  //To do Implement your code + " Finish");


For more Detail Information. Please refer below document from SpringFramwork.

If you are interested in Spring batch and quartz, Please refer below link
Spring batch and quartz


  1. Nice post Jack... Thanks for posting this.

    By the way, if I want to take the cron value from any database or property, how to do it?

    1. Hi ffredie,
      Thanks for visiting.
      What I can try is, ussing SPEL which can get value from property or use Spring Java configuration which can configure property and bean by java code.
      I haven't tried though. It might work.
      Sorry, I don't have a sample.

  2. Completely disagree with "quartz + Spring" was not easy...

    have you tried this?