为了账号安全,请及时绑定邮箱和手机立即绑定

Java Quartz定时任务的实现

标签:
Java
Java Quartz定时任务的实现

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

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

二、根据quartz.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/utilhttp://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>

quartz.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对应的XQuartzService

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 XQuartzService {

}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
7114
获赞与收藏
545

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消