首先是一个简单的Java类:
public class QuartzJob { private static Logger logger = LoggerFactory.getLogger(QuartzJob.class); // @Autowired MonitorDetailService monitorDetailService; @SuppressWarnings("static-access") public void execute() { /* 业务逻辑 */ System.out.println("time is :" + System.currentTimeMillis()); } }
再贴出配置文件:applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-lazy-init="false"> <description>Quartz的本地Cron式执行任务配置</description> <!-- Cron式Trigger定义 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="cronJobDetail" /> <property name="cronExpression" value="1,3,5,7,9,11,18,20,25,30,33,37,40,45,49,50,53,55,58 * * * * ?" /> </bean> <!-- Cron JobDetajil, 基于MethodInvokingJobDetailFactoryBean调用普通Spring Bean --> <bean id="cronJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="cronJob" /> <property name="targetMethod" value="execute" /> <!-- 前后两次任务是否并发执行 --> <property name="concurrent" value="true" /> </bean> <!-- job --> <bean id="cronJob" class="***.schedule.QuartzJob" /> <!-- Quartz任务调度 --> <bean id="localQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false"> <!-- Triggers集成 --> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> <!-- Quartz属性配置 --> <property name="quartzProperties"> <props> <prop key="org.quartz.threadPool.threadCount">3</prop> </props> </property> <!-- 启动时延期3秒开始任务 --> <property name="startupDelay" value="3" /> </bean> </beans>
将配置文件添加到WEB-INF/web.xml中的context-param里
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml classpath:applicationContext-quartz.xml </param-value> </context-param>
结果启动时一直报错,最后发现Quartz的版本有问题,于是把2.3的Quartz换成了1.6.6,之后启动正常。
另:也可以写一个类实现ServletContextListener,来启动Quartz。我在项目中启动Mina时也用这种方法。
如:
public class QuartzService { public static void main(String[] args) { System.out.println("测试任务调度开始..."); ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext-quartz.xml"); // 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化 // context.getBean("startQuertz"); System.out.print("测试任务调度结束!/n"); } }
一个简单的ServletContextListener的实现:
public class ContainerStartListener implements ServletContextListener{ /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { } /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) */ @SuppressWarnings("static-access") public void contextInitialized(ServletContextEvent arg0) { QuartzService quartzService = new QuartzService(); quartzService.main(null); } }
在web.xml中加入一个listener即可
<listener> <listener-class>***.ContainerStartListener</listener-class> </listener>
之后就可以正常启动了。通过spring来管理Quartz可以避开Quartz的复杂性。
对于spring和Quartz的整合还可以参考这篇文字 http://blog.csdn.net/chineselly/article/details/6400254