Quartz定时器的使用

网址:https://www.jianshu.com/p/2a5d3b6336ba
Java爬虫:https://blog.csdn.net/z694644032/article/details/102363914
一 配置方式的定时器的执行
1:pom文件中插入Quartz依赖

org.springframework.boot
spring-boot-starter-quartz

2:在resources下面插入两个xml

3:spring-mvc.xml的配置如下

<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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task.xsd">
    <!--定义自动注解的配置-->
    <context:annotation-config/>
    <!-- 利用import引入定时器的文件 -->
    <!--    为了防止spring MVC写起来很大,所以引用这个来-->
    <import resource="spring-quartz.xml"/>

<!--    <bean id="druidStatFilter" class="org.springframework.boot.web.servlet.ServletRegistrationBean">-

->

4;spring-quartz.xml配置如下

<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">
    <!--spring-quart主要配置四个,第一个就是定时器的类配置-->
    <bean id="taskJob" class="cesi.demo.quartz.TestTask"/>

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <!--            bean是上面类的那个定时器的id-->
            <ref bean="taskJob"/>
        </property>
        <!--        上面类里面定义的方法-->
        <property name="targetMethod">
            <value>run</value>
        </property>
    </bean>

    <!--  调度触发器 -->
    <bean id="myTrigger"
          class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="jobDetail"/>
        </property>
        <property name="cronExpression">
            <!-- 触发规则表达式-->
            <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>

5.执行的类代码如下

package cesi.demo.quartz;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class TestTask {

    //日志对象
    private static final Logger logger = LogManager.getLogger(TestTask.class);

    public void run() {
        logger.info("定时器运行了!!!");
    }
}

6.application中的配置
classpath:spring-mvc.xml这个必须要引入的

package cesi.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations={"classpath:spring-mvc.xml"})

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

二 注解方式配置定时任务
通过配置@Scheduled(fixedDelay = 100*30)定时执行任务
application不用添加配置,但是一般项目采用的是xml统一配置因为方便查看

package cesi.demo.quartz;

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

//先将方法注入spring容器中
@Component
//定时器的注入
@Configurable
@EnableScheduling
public class TestQuartz2 {
    
    @Scheduled(fixedDelay = 100*30)
    public void test() {
        System.out.println("定时器第一个执行");
    }

    //每5秒执行一次
    @Scheduled(cron = "*/5 * *  * * * ")
    public void test2() {
        System.out.println("定时器第二个执行");
    }
    //每1分针执行一次
    @Scheduled(cron = "0 */1  *  * * * ")
    public void test3() {
        System.out.println("定时器第三个执行");
    }
}

三 xxlljob配置的方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值