springboot学习系列四:springboot集成quartz

目录

 

pom.xml中引入依赖

定时任务配置文件

定时任务注解方式创建


pom.xml中引入依赖

<dependency>

            <groupId>org.quartz-scheduler</groupId>

            <artifactId>quartz</artifactId>

            <version>2.2.3</version>

</dependency>

定时任务配置文件

使用xml的方式创建定时器。

(1)spring-mvc.xml配置文件(src/main/resources目录下)

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/>

    <import resource="spring-quartz.xml"/>
</beans>

(2)spring-quartz.xml配置文件(src/main/resources目录下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 定义job对象 -->
    <bean id="myjob" class="com.example.demo.quartz.TaskTest"></bean>
    <!-- 注册jobDetail,通过反射调用自定义任务对象 -->
    <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 目标对象 -->
        <property name="targetObject">
            <ref bean="myjob"/>
        </property>
        <property name="targetMethod">
            <value>sendMessage</value>
        </property>
    </bean>
    <!-- CronTriggerFactoryBean:触发器 触发什么任务 什么时间触发 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 配置触发哪个任务 -->
        <property name="jobDetail">
            <ref bean="myJobDetail"/>
        </property>
        <!-- 什么时间触发任务 cronExpression:cron表达式 每隔2分钟执行一次任务 -->
        <property name="cronExpression">
            <value>0 0/2 * * * ?</value>
        </property>
    </bean>
    <!-- SchedulerFactoryBean:配置计划调度器容器 -->
    <bean lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>

</beans>

(3)定时器类

package com.example.demo.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TaskTest {

    public void sendMessage(){

        System.out.println("方法执行了:"+new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()));
    }
}

(4)测试代码效果

定时任务注解方式创建

使用注解的方式创建定时任务。

(1)注解测试代码

package com.example.demo.quartz;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Configurable
@EnableScheduling
public class TaskOfComponentTest {

    @Scheduled(cron = "*/5 * *  * * *")
    public void sendMessage(){

        System.out.println("每隔5秒执行一次");
    }

}

(2)测试代码效果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值