Java Quarzt定时任务的实现

来源:互联网 发布:卫星通信网络 编辑:程序博客网 时间:2024/06/01 17:27
Java Quarzt定时任务的实现


首先,实现quarzt定时任务只需要俩个条件:

一、在web项目中新建一个quarzt.xml文件,下面是XML文件的示例代码。

二、根据quarzt.xml文件中的任务写对应的service任务实现类

<?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"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><context:annotation-config/>    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->    <bean id="startQuertz" lazy-init="false" autowire="no"        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>            <ref bean="chipinServiceQuartzTiggerDay"/><!-- 清算用户下注记录任务 -->            <ref bean="prizeServiceQuartzTiggerDay"/><!-- 开奖任务 -->            <ref bean="expernServiceQuartzTiggerDay"/><!-- 赔付任务 -->            </list>        </property>    </bean><!--清算用户下注记录任务 --><bean id="prizeService"   class="com.zhugong.pk.repositorie.PrizeService"/><bean id="chipinServiceQuartzDitail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="prizeService"></property><!-- 指定任务类 --><property name="targetMethod" value="chipin"></property><!-- 清算用户下注记录任务方法 --></bean><bean id="chipinServiceQuartzTiggerDay" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="chipinServiceQuartzDitail"></property><property name="cronExpression"> <value>30 4,4/5 9/1 * * ?</value> <!-- 30 4,4/5 0/1 * * ? 30  0/4 0/1 * * ? --></property></bean><!--开奖任务 --><bean id="prizeServiceQuartzDitail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="prizeService"></property><!-- 指定任务类 --><property name="targetMethod" value="prize"></property><!-- 开奖任务方法 --></bean><bean id="prizeServiceQuartzTiggerDay" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="prizeServiceQuartzDitail"></property><property name="cronExpression"> <value>35 4,4/5 9/1 * * ?</value>  <!-- 0 0/5 0/1 * * ? --></property></bean><!--赔付任务 --><bean id="expernServiceQuartzDitail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="prizeService"></property><!-- 指定任务类 --><property name="targetMethod" value="expern"></property><!-- 赔付任务方法 --></bean><bean id="expernServiceQuartzTiggerDay" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="expernServiceQuartzDitail"></property><property name="cronExpression"> <value>55 4,4/5 9/1 * * ?</value>  <!-- 0 0/5 0/1 * * ? --></property></bean><!-- 0 0/30 0/1 * * ? 代表每天每30分钟运行一次 --><!-- 0 0/5 0/1 * * ? 代表每天每5分钟运行一次 --><!-- 年 月 周 日 时 分 秒 --></beans>



quarzt.xml文件需要注意俩个点:

  • org.springframework.scheduling.quartz.SchedulerFactoryBean的任务调度
  • cronExpression表达式的理解和使用

<!-- 0 0/30 0/1 * * ? 代表每天每30分钟运行一次 -->

从左到右为 : <!-- 年 月 周 日 时 分 秒 -->


“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。 
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”. 
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 
“L”字符指定在月或者星期中的某天(最后一天)。即“Last ”的缩写。但是在星期和月中“L”表示不同的意思,如:在月子段中“L”指月份的最后一天-1月31日,2月28日,如果在星期字段中则简单的表示为“7”或者“SAT”。如果在星期字段中在某个value值得后面,则表示“某月的最后一个星期value”,如“6L”表示某月的最后一个星期五。
“W”字符只能用在月份字段中,该字段指定了离指定日期最近的那个星期日。
“#”字符只能用在星期字段,该字段指定了第几个星期value在某月中


quarzt对应的XQuarztService

package com.xunxin.config;/** * Copyright © 2017 noseparte(Libra) © Like the wind, like rain * @Author Noseparte * @Compile 2017年11月30日 -- 下午3:44:57 * @Version 1.0 * @Description   定时调度任务 */public class XQuarztService {      }



      

About Me:

  • Github地址:https://github.com/noseparte 
  • Email: noseparte@aliyun.com     有java与hadoop相关的技术问题,可以发私信与我交流。
  • NPM地址: https://www.npmjs.com/~noseparte
  • WebSite: http://www.noseparte.com/  Copyright © 2017 noseparte



原创粉丝点击