基于spring的quartz定时任务的配置详述

        前段时间由于任务需要,需要加一个定时任务,于是东拼西凑找了好多篇文章博客之类的,一直没有弄好,用了timer,在测视的类里用的一点问题没有,但是一到业务类就会出问题,于是就暂时放弃了这个方法(后来找到了问题,暂且不提),用到了quartz这个类,感觉很好用,跟大家分享一下

一  、基于spring的quartz定时任务的详述

        1, 首先需要的,就是先写好自己的业务类,声明自己的业务方法(本人建议最好声明一个定时包,把要用的方法写到里面,以便以后容易找到)

         2 ,剩下的基本就是靠配置了,首先创建一个xml的配置文件,引入配置信息,在这里,强调引入的三个类,一个是quartz里面的MethodInvokingJobDetailFactoryBean类,看名字就可以知道,是唤醒方法的工厂,这个类主要是配置你的定时业务类和方法,(完全限定名 org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)然后,就是quartz里面的CronTriggerBean类,这个类就是进行引用你上面配置的类和方法的id,进行定时任务的时间配置,也可以说是定时任务的配置大脑。(完全限定名org.springframework.scheduling.quartz.CronTriggerBean)在这里,要声明一点,定时的值需要的quartz语法,所以大家可以去找一下,我在下面会友情给出几个常用的方法。配置好了时间任务以后,然后就是执行了,在这里,不出所料,用到的就是Scheduler打头的SchedulerFactoryBean类,这个方法起到的是调度定时任务的作用,你写到的n个定时任务都可以用它进行选择性执行(完全限定名org.springframework.scheduling.quartz.SchedulerFactoryBean)

          3,配置完了上述几点以后,定时任务的xml文件基本上是配置完了,接下来就要进行引用了,在这里,声明一下,引用有两种方式,一种是基于web.xml文件配置的,一种是基于spring的配置文件配置的,web.xml配置就是常规的引入,spring的也很简单,下面我会给出代码详解

    二  具体的配置文件  

           1,编写定时任务

                 

package com.lcworld.yayi.timer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.lcworld.yayi.dao.UserDao;
import com.lcworld.yayi.model.Appointment;
import com.lcworld.yayi.model.User;
import com.lcworld.yayi.utils.msg.MsgSender;
import com.lcworld.yayi.utils.msg.SendStatus;

public class TwohoursTimer {
	@Autowired
	private UserDao userDao;
	
	private final Log log=LogFactory.getLog(TwohoursTimer.class);
	
	public void execute(){
		
		
		List<Appointment> appointmentList= userDao.sendUserMsg();
	     // System.out.println(appointmentList.toString());
		if(appointmentList != null){
			for(Appointment appointment:appointmentList){
				
				log.info("获得订单的id开始===>>>>"+appointment.getId());
				Date treatment_time=appointment.getTreatment_time();
				int uid = appointment.getUid();
				int clinicid = appointment.getClinicId();
				Date datetime = new Date();
					
					Long  longtreatment = treatment_time.getTime();
					Long  datelong = datetime.getTime();
					
					final Long twohoursbefor = (long)(1000*60*60*2+1000*60);
					final Long twohoursafter = (long)(1000*60*60*2-1000*60);
					
					long datecha = 0;
					
					if(treatment_time != null){
						datecha = longtreatment-datelong;
					}
					// System.out.println(datecha);
					if(datecha <= twohoursbefor && datecha > twohoursafter){
						User user = userDao.selectByPrimaryKey(uid);
						String adress = userDao.selectAdress(clinicid);
						SendStatus ss=null;
						ss = MsgSender.senMsgTimmer(user.getMobile(), user.getNickname(),adress);
						System.out.println(ss.getCode()+":"+ss.getMsg()); 
					}
				log.info("获得订单Id结束----》》"+appointment.getId());
			}
		}
	}
}
   2配置定时任务的xml文件
<span style="background-color: rgb(255, 255, 255);">      </span><pre name="code" class="html"><span style="background-color: rgb(255, 255, 255);"><?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
		<bean id="TwoHoursTask" class="com.lcworld.yayi.timer.TwohoursTimer" />
		
		<bean id="TwohoursTimerTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
			<property name="targetObject">
				<ref bean="TwoHoursTask"></ref>
			</property>
			<property name="targetMethod">
				<value>execute</value>
			</property>
		</bean>
		
		<bean id="TwohoursTimerTaskJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
			<property name="jobDetail">
				<ref bean="TwohoursTimerTaskJob"></ref>
			</property>
			<property name="cronExpression" value="0 0/30 7-18 * * ?"></property>
		</bean>
		
		<bean id="CalTimerTaskJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="TwohoursTimerTaskJobTrigger" /></span>
                           <!-- <ref bean="MonthlyTimerTaskJobTrigger" /> -->//可以选择性注释
			</list>
		</property>
	</bean>	
</beans></span>
   3 引用定时任务xml文件
     <import resource="classpath:CalTimerTask.xml"/>  //在spring的配置文件里复制这行代码,把CalTimerTask换成你自己的就ok了
</pre><pre name="code" class="html">   最后,友情提示一下quartz的常用语法
    0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
    0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 
    0 0 12 ? * WED 表示每个星期三中午12点  
   "0 0 12 * * ?" 每天中午12点触发  
   "0 15 10 ? * *" 每天上午10:15触发  
   "0 15 10 * * ?" 每天上午10:15触发  
   "0 15 10 * * ? *" 每天上午10:15触发  
   "0 15 10 * * ? 2005" 2005年的每天上午10:15触发  
   "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发  
   "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发  
   "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发  
   "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发  
   "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发  
   "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发  
   "0 15 10 15 * ?" 每月15日上午10:15触发  
   "0 15 10 L * ?" 每月最后一日的上午10:15触发  
   "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发  
   "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发  
   "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 

 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KeepMoving00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值