spring quartz 定时器

来源:互联网 发布:注册阿里云个人邮箱 编辑:程序博客网 时间:2024/05/16 12:36

spring 4 + quartz 2.2




业务方法

package com.ershuai.quartz;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.ershuai.util.DateUtil;/** * 测试定时器 * @author ershuai * @date 2016年7月1日 上午11:31:19  * @version V1.0 */@Componentpublic class QuartzController {private static IxxxxxxService xxxxxxService;@Autowired(required = true)public void setXxxxxxService(IxxxxxxService xxxxxxService) {QuartzController.xxxxxxService = xxxxxxService;}/** *  * @author ershuai * @date 2016年7月1日 上午11:31:49  * @version V1.0  * @throws Exception */public void testQuartzMethod() throws Exception {System.out.println("开始时间:" + DateUtil.getTime(5));System.out.println("执行任务ing…");XxxxxxEntity xxxxxxEntity = new XxxxxxEntity();QuartzController.xxxxxxService.saveOrUpdate(xxxxxxEntity);System.out.println("结束时间:" + DateUtil.getTime(5));    }}


application-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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id="testQuartz" class="com.ershuai.quartz.QuartzController" /><!-- 结佣任务 --><bean id="testQuartzTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="testQuartz" /></property><property name="targetMethod"><value>testQuartzMethod</value></property></bean><!-- 定义触发时间 --><bean id="testQuartzTaskTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail"><ref bean="testQuartzTask" /></property><!-- cron表达式 --><property name="cronExpression"><!-- 每10秒触发 --><value>0/10 * * * * ?</value></property></bean><!-- 管理类 --><bean id="testQuartzStart" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="testQuartzTaskTime" /></list></property></bean></beans>


启动就可以玩了!


0 0
原创粉丝点击