0
点赞
收藏
分享

微信扫一扫

SSM整合Quartz(定时任务)

小时候是个乖乖 2022-04-17 阅读 75

前言

正文:SSM如何整合Quartz?

        <!--  spring-context核心类和核心支持类  spring-webmvc spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.3.16</version>
        </dependency>
        <!--  定时任务-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>

 参考文章:定时任务轮询报错:Cannot find class [org.springframework.scheduling.quartz.CronTriggerBean]_Lu、ck的博客-CSDN博客

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

    <context:component-scan base-package="com.lsq.utils" />

    <!-- 注入任务处理类bean -->
    <bean id="myJob" class="com.lsq.utils.MyJob"></bean>

    <!-- 任务触发器详细信息bean -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJob"></property>
        <property name="targetMethod" value="runJob"></property>
        <property name="concurrent" value="false" /><!-- 作业不并发调度  -->
    </bean>
<!--    <bean id="otherDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">-->
<!--        <property name="targetObject" ref="otherJob"></property>-->
<!--        <property name="targetMethod" value="execute"></property>-->
<!--        <property name="concurrent" value="false" />&lt;!&ndash; 作业不并发调度  &ndash;&gt;-->
<!--    </bean>-->

    <!-- 定义trigger 触发器 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>
<!--    <bean id="otherTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">-->
<!--        <property name="jobDetail" ref="otherDetail"></property>-->
<!--        <property name="cronExpression" value="${crond.otherJob}"></property>-->
<!--    </bean>-->

    <!-- 设置触发器调度工厂 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger"/>
<!--                <ref bean="otherTrigger"/>-->
            </list>
        </property>
    </bean>
</beans>

 

package com.lsq.utils;

import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("MyJob")
public class MyJob {
    public void runJob() {
        /**
         * 定时任务的具体逻辑,本处以每五秒输出时间为例
         */
        System.out.println("现在的时间是:"+new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
    }
}

其中触发器中有着控制如何定时的功能(几月几号什么时间每隔多久干什么事.......)此处不做过多解释,叫 Corn表达式可以通过以下链接自行理解:

quartz/Cron/Crontab表达式在线校验工具-BeJSON.com

 参考文章:ssm框架整合quartz实现定时任务_小码哥cc的博客-CSDN博客

举报

相关推荐

0 条评论