第十三章 Quartz

       Quartz是一个良好的任务调度中间插件

1. 引入springquartzjar

2. 创建定时任务类

package com.timeout;

import java.sql.Time;

/*
 * 	定时任务类
 */
public class StudentDay {
	public void study(){
		// 获取时间
		Time time = new Time(System.currentTimeMillis());
		// 执行任务
		System.out.println(time+" : 学习中.........");
	}
}

3. 配置spring注入Quartz

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	">
	
	<!-- 注入定时任务类 -->
	<bean id="stuDay" class="com.timeout.StudentDay"></bean>
	
	<!-- 定义执行任务bean的类 -->
	<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<!-- 配置调用的类 -->
		<property name="targetObject" ref="stuDay"></property>
		<!-- 配置执行的方法 -->
		<property name="targetMethod" value="study"></property>
	</bean>
	
	<!-- 定义触发器 -->
	<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobDetail"></property>
		<!-- 触发Corn时间表达式 -->
		<property name="cronExpression" value="0/1 * * * * ?"></property>
	</bean>
	
	<!-- 启动类,执行触发器 -->
	<bean id="starQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="doTime" />
			</list>
		</property>
	</bean>
	
	
</beans>

4. 测试

package com.timeout;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
 * 	测试Quartz
 */
public class Test {
	public static void main(String[] args) {
		/*
		 * 	加载Quartz配置文件xml,spring自动调用bean执行
		 */
		ApplicationContext ac = new ClassPathXmlApplicationContext("springQuartz.xml");
		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值