使用Spring Quartz 定时任务
使用Spring Quartz 定时任务时需要以下几点:
1、在项目中添加 quartz.1.6.5.jar
2、添加一个文件名为:applicationContext-quartz.xml的文件。内容为:
<description>Quartz的定时任务配置</description><!-- Quartz集成工厂 --> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --><bean lazy-init="false"><!-- Triggers集成 --><property name="triggers"><list> <ref bean="doJobTrigger"/></list></property><!-- Quartz配置 --><property name="quartzProperties"> <props> <prop key="org.quartz.threadPool.threadCount">5</prop> </props></property></bean><!-- 要调用的工作类 --><bean id="dojob" ref="dojob" /><property name="targetMethod" value="doJob" /></bean><bean id="doJobTrigger" ref="jobtask"></property><!-- 每5秒钟执行一次 --> <!-- 此处时间的设置为:--> <!-- 秒 0-59 , - * /--> <!-- 分 0-59 , - * /--> <!-- 小时 0-23 , - * /--> <!-- 日期 1-31 , - * ? / L W C--> <!-- 月份 1-12 或者 JAN-DEC , - * /--> <!-- 星期 1-7 或者 SUN-SAT , - * ? / L C #--> <!-- 年(可选) 留空, 1970-2099 , - * / --><property name="cronExpression"><value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value></property></bean>
import java.text.SimpleDateFormat;import java.util.Date;//直接调用public class QuartzTest {public void doJob(String[] args) {SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String time = d.format(new Date());System.err.println("调用时间:"+time);}}