SpringBoot定时任务xml配置

Stella981
• 阅读 552

    由于定时任务需要工程实施人员进行配置,写在Java代码里面不利于维护,故采用SpringBoot集成Spring XML文件进行配置,属于新瓶装旧酒。

1 新建定时任务类

package com.duhongming.springbootscheduled.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DataScheduledTask {

    private Integer i;
    private Double d;
    private String s;
    private Boolean b;

    public DataScheduledTask(Integer i,Double d,String s,Boolean b){
        super();
        this.i = i;
        this.d = d;
        this.b = b;
        this.s = s;
    }

    /**
     * xml方式
     */
    public void springContextXmlexecute(){
        log.info("i:{},d:{},b:{},s:{}",i,d,b,s);
    }

    /**
     * 注解方式
     */
    @Scheduled(cron = "${config.cron}" )
    public void scheduled() {
        log.info("scheduled");
    }

}

2 在resources下面配置spring-context.xml

<?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: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/task http://www.springframework.org/schema/task/spring-task.xsd"
       default-lazy-init="true">

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">          
        <!-- 核心线程数,默认为1 -->          
        <property name="corePoolSize" value="10" />          
        <!-- 最大线程数,默认为Integer.MAX_VALUE -->          
        <property name="maxPoolSize" value="50" />          
        <!-- 队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE -->          
        <property name="queueCapacity" value="1000" />          
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->          
        <property name="keepAliveSeconds" value="300" />          
        <!-- 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->          
        <property name="rejectedExecutionHandler">          
            <!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->          
            <!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->          
            <!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->          
            <!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->          
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />          
        </property>          
    </bean> 
    
    <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
    <task:scheduler id="scheduler" pool-size="24" />
    
    <task:annotation-driven scheduler="scheduler" executor="taskExecutor" proxy-target-class="true"/>
    
    <!-- * * * * * ? 测试专用:一秒一次-->
    <task:scheduled-tasks>      
        <task:scheduled ref="dataScheduledTask" method="springContextXmlexecute" cron="0/1 * * * * ?" /> <!-- 一秒钟一次 -->
    </task:scheduled-tasks>

    <!-- DataScheduledTask注入 -->
    <bean id="dataScheduledTask" class="com.duhongming.springbootscheduled.scheduled.DataScheduledTask">
        <constructor-arg name="i" value="1" /><!-- Integer -->
        <constructor-arg name="d" value="1.01" /><!-- Double -->
        <constructor-arg name="s" value="字符串"/><!-- String -->
        <constructor-arg name="b" value="true" /><!-- Boolean -->
    </bean>
</beans>

3 在resources下面配置application.properties

#在jar内
spring.context.xml.path=classpath:spring-context.xml
#在jar外
#spring.context.xml.path=file:spring-context.xml

#注解方式配置参数
config.cron=0/5 * * * * ?

4 新建一个包config,以及一个类ConfigClass

package com.duhongming.springbootscheduled.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations={"${spring.context.xml.path}"})
public class ConfigClass {}

5 整体结构如下

SpringBoot定时任务xml配置

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Easter79 Easter79
2年前
SpringBoot定时任务xml配置
    由于定时任务需要工程实施人员进行配置,写在Java代码里面不利于维护,故采用SpringBoot集成SpringXML文件进行配置,属于新瓶装旧酒。1新建定时任务类packagecom.duhongming.springbootscheduled.scheduled;importlomb
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
京东云开发者 京东云开发者
6个月前
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这