定时任务组件Quartz

定时任务组件Quartz

Quartz是Job scheduling(作业调度)领域的一个开源项目,Quartz既可以单独使用也可以跟spring框架整合使用,在实际开发中一般会使用后者。使用Quartz可以开发一个或者多个定时任务,每个定时任务可以单独指定执行的时间,例如每隔1小时执行一次、每个月第一天上午10点执行一次、每个月最后一天下午5点执行一次等。
使用步骤
<1>引入依赖

<dependencies>
	<dependency>
        <groupId>cn.itcast</groupId>
        <artifactId>health_interface</artifactId>
        <version>1.0-SNAPSHOT</version>
     </dependency>
	<dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
	</dependency>
	<dependency>
		<groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
	</dependency>
</dependencies>

<2>配置文件spring_job
第一种:xml配置
提示更改:自定义Job,目标方法

<?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.xsd
							http://www.springframework.org/schema/context
							http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启spring注解使用-->
    <context:annotation-config></context:annotation-config>
    <!--注册自定义Job-->
    <bean id="Job" class="*****.Job"></bean>

    <bean id="jobDetail"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 注入目标对象 -->
        <property name="targetObject" ref="Job"/>
        <!-- 注入目标方法 -->
        <property name="targetMethod" value="*****"/>
    </bean>
    <!-- 注册一个触发器,指定任务触发的时间 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 注入JobDetail -->
        <property name="jobDetail" ref="jobDetail"/>
        <!-- 指定触发的时间,基于Cron表达式 -->
        <property name="cronExpression">
            <!--
            <value>0 0 2 * * ?</value>
            -->
            <value>0/10 * * * * ?</value>
        </property>
    </bean>
    <!-- 注册一个统一的调度工厂,通过这个调度工厂调度任务 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 注入多个触发器 -->
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>
</beans>

第二种
//注解配置
提示更改:扫描包

<?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:task="http://www.springframework.org/schema/task"
       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/task http://www.springframework.org/schema/task/spring-task.xsd">
    <!--开启spring注解使用-->
    <context:annotation-config></context:annotation-config>
  <context:component-scan base-package="***.jobs"></context:component-scan>
    <!-- 配置处理定时任务的线程池 -->
    <task:executor id="executor" pool-size="10"/>
    <!--  配置处理 异步定时任务的  线程池 -->
    <task:scheduler id="scheduler" pool-size="10"/>
    <!-- 启用annotation方式 -->
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
</beans>

注解配合使用
job方法上配置注解@Scheduled

@Scheduled(cron ="表达式" )
public void job(){
}

<3>cron表达式生成器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值