Java Quarzt定时任务的实现

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.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
				http://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:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值