Quartz入门

       Quartz是一个完全由Java编写的开源作业调度框架,当你想实现定时做些事情的时候,它就派上用场啦!目前Quartz比较稳定的版本是2.2.1,所以我这里就以这个版本为例,如果你使用Quartz2.x系列,那你的Spring版本必须3.1版本及以上(假如你需要将Quartz跟Spring整合的话),Quartz并不一定需要跟Spring整合哈,它完全可以脱离Spring单独工作,只是Spring目前太流行,所以大家都喜欢将其他框架往Spring里整合。

      下面是Quartz的一个入门级别的简单示例:

       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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
             http://www.springframework.org/schema/context    
             http://www.springframework.org/schema/context/spring-context-3.1.xsd   
             http://www.springframework.org/schema/aop    
             http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
             http://www.springframework.org/schema/tx    
             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd   
             http://www.springframework.org/schema/mvc    
             http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
             http://www.springframework.org/schema/context    
             http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-autowire="byName" default-lazy-init="true">

	<bean id="taskJob" class="com.yida.framework.test.FirstJob" />
	<bean id="jobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="group" value="job_work" />
		<property name="name" value="job_work_name" />
		<!--false表示等上一个任务执行完后再开启新的任务 -->
		<property name="concurrent" value="false" />
		<property name="targetObject">
			<ref bean="taskJob" />
		</property>
		<property name="targetMethod">
			<value>run</value>
		</property>
	</bean>
	<!-- 调度触发器 -->
	<bean id="myTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="name" value="work_default_name" />
		<property name="group" value="work_default" />
		<property name="jobDetail">
			<ref bean="jobDetail" />
		</property>
		<property name="cronExpression">
			<value>0/3 * * * * ?</value>
		</property>
	</bean>
	<!-- 调度工厂 -->
	<bean id="scheduler" autowire="no"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="myTrigger" />
			</list>
		</property>
	</bean>
</beans>

   

    web.xml里配置加载spring的相关配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/com/yida/framework/base/config/spring/application*.xml
		</param-value>
	</context-param>

    编写一个简单的任务测试类:

package com.yida.framework.test;

public class FirstJob {
	public void run(String[] args) {
		System.out.println("我的第一个测试任务。");
	}
}

    最后部署你的项目,启动tomcat,任务就会自动执行啦!

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值