Spring整合quartz定时任务

简介: Spring整合quartz定时任务

如有需要可以加我Q群【308742428】大家一起讨论技术,提供技术支持。

后面会不定时为大家更新文章,敬请期待。

今天我们来分享一波在spring项目使用quartz做定时任务。

首先我这里的项目已经是一个可以跑起来的完整项目,web.xml里面的配置我就不贴出来了。

1.新建一个类ConfigConsts

我们用来放cron表达式:
更多cron表达式

 
 
 
public abstract class ConfigConsts {
 
    /** 30分钟执行一次 */
    public static final String quartzInterval = "0 0/30 * * * ?";
 
    /** 每天凌晨1:30分执行*/
    public static final String quartzCustomerInterval = "0 30 1 * * ?";
 
 
    /** 每天凌晨1:00分执行*/
    public static final String quartzMaterialInterval = "0 0 1 * * ?";
    
    /** 每天凌晨2:00分执行*/
    public static final String quartzSupplierInterval = "0 0 2 * * ?";
 
   
}

2.新建一个QuartzHandler类来实现我们的代码逻辑

 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aowang.framework.daoComp.Jconn;
import com.aowang.framework.daoComp.jComp;
import com.aowang.utils.APIUtils;
import com.aowang.utils.Constants;
import com.aowang.utils.DateUtil;
import com.aowang.utils.Utils;
import com.aowang.utils.http.HttpClientResult;
import com.aowang.utils.http.HttpClientUtils;
 
import net.sf.json.JSONArray;
 
/**
 * 描述:定时任务调度
 * 
 * @author dsn
 * @date 2020年8月28日 下午2:57:41
 */
public class QuartzHandler {
 
    private static final Logger logger = LoggerFactory.getLogger(QuartzHandler.class);
 
    private static boolean isFirst = true;// 第一次执行定时任务
    @Autowired
    private Jconn jcon; // 数据库组件
 
    private Map<String, Object> map = new HashMap<String, Object>();
 
    private static String startDate = "20130101";
 
    /**
     * Description:定时执行拉取客户主数据处理 <BR>
     * 
     * @author dsn
     * @date 2020年8月28日 下午11:57:28
     * @version 1.0
     * @throws Exception
     */
    public void run4Customer() throws Exception {
        // 定时执行1
        System.out.println("定时任务开启----------------------------");
        //这里面就可以写代码逻辑
 
    }
 
 
}

3.新建一个application-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.xsd">
    
    <!-- 要调用的工作类 -->      
    <bean id="mainClass" class="com.aowang.quartz.QuartzHandler">
    </bean>
    
     <!-- 任务配置列表 -->  
    <bean id="task_customer"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 指定任务类 -->
        <property name="targetObject" ref="mainClass" />
        <!-- 指定任务执行的方法 -->
        <property name="targetMethod" value="run4Customer" />
    </bean>
 
    <!-- 将运行时间策略常量放入bean池 -->
    <bean id="interval_customer" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> 
        <property name="staticField" value="com.aowang.quartz.ConfigConsts.quartzInterval"/> 
    </bean> 
 
    <!-- 触发器配置  时间指定 -->  
    <bean id="trigger_customer"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="task_customer" />
        <property name="cronExpression" ref="interval_customer" />
    </bean>
 
 
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->  
    <bean lazy-init="true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 触发器列表    -->  
                <ref bean="trigger_customer" />
 
                
            </list>
        </property>
    </bean>
 
</beans>

4.在applicationContent.xml中引入第3步新建的xml

其实就是这么的简单,完事。

相关文章
|
监控 Java BI
《深入理解Spring》定时任务——自动化调度的时间管理者
Spring定时任务通过@Scheduled注解和Cron表达式实现灵活调度,支持固定频率、延迟执行及动态配置,结合线程池与异常处理可提升可靠性,适用于报表生成、健康检查等场景,助力企业级应用自动化。
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
779 1
|
Java 调度 Spring
Spring之定时任务基本使用篇
本文介绍了在Spring Boot项目中使用定时任务的基本方法。主要通过`@Scheduled`注解实现,需添加`@EnableScheduling`开启定时任务功能。文中详细解析了Cron表达式的语法及常见实例,如每秒、每天特定时间执行等。此外,还探讨了多个定时任务的执行方式(并行或串行)及其潜在问题,并留待后续深入讨论。
1176 64
|
Java Spring
使用 Spring Boot 多个定时任务阻塞问题的解决方案
我是小假 期待与你的下一次相遇 ~
772 5
|
存储 前端开发 Java
|
监控 Java 调度
SpringBoot中@Scheduled和Quartz的区别是什么?分布式定时任务框架选型实战
本文对比分析了SpringBoot中的`@Scheduled`与Quartz定时任务框架。`@Scheduled`轻量易用,适合单机简单场景,但存在多实例重复执行、无持久化等缺陷;Quartz功能强大,支持分布式调度、任务持久化、动态调整和失败重试,适用于复杂企业级需求。文章通过特性对比、代码示例及常见问题解答,帮助开发者理解两者差异,合理选择方案。记住口诀:单机简单用注解,多节点上Quartz;若是任务要可靠,持久化配置不能少。
1401 4
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
16831 136
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
1072 2
|
存储 Java API
简单两步,Spring Boot 写死的定时任务也能动态设置:技术干货分享
【10月更文挑战第4天】在Spring Boot开发中,定时任务通常通过@Scheduled注解来实现,这种方式简单直接,但存在一个显著的限制:任务的执行时间或频率在编译时就已经确定,无法在运行时动态调整。然而,在实际工作中,我们往往需要根据业务需求或外部条件的变化来动态调整定时任务的执行计划。本文将分享一个简单两步的解决方案,让你的Spring Boot应用中的定时任务也能动态设置,从而满足更灵活的业务需求。
1500 4
|
Java 关系型数据库 MySQL
SpringBoot 集成 Quartz + MySQL
SpringBoot 集成 Quartz + MySQL
687 1