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="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <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 --> </beans>
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; @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() { logger.info(this.getClass().getName() + " Start"); //To do Implement your code logger.info(this.getClass().getName() + " Finish"); } }
For more Detail Information. Please refer below document from SpringFramwork.
http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/scheduling.html
If you are interested in Spring batch and quartz, Please refer below link
Spring batch and quartz
Nice post Jack... Thanks for posting this.
ReplyDeleteBy the way, if I want to take the cron value from any database or property, how to do it?
Thanks
Hi ffredie,
DeleteThanks 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.
BR,
Completely disagree with "quartz + Spring" was not easy...
ReplyDeletehave you tried this?
http://briansjavablog.blogspot.com/2012/09/spring-quartz-tutorial.html