Springboot集成SpringTask

本文介绍如何在SpringBoot项目中配置并使用定时任务。通过两种方式实现:一是基于注解的方式,只需在启动类上添加@EnableScheduling注解,并在任务类上使用@Scheduled注解;二是基于XML配置文件的方式,通过创建applicationContext-task.xml配置文件,定义任务调度器及具体任务。

Springboot集成SpringTask

基于注解配置

第一步

在springboot启动类中添加注解@EnableScheduling

package top.simba1949;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author simba@onlying.cn
 * @date 2018/7/9 20:08
 */
@SpringBootApplication
@EnableScheduling // 支持schedule定时任务
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

第二步

编写定时任务

package top.simba1949.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * @author simba@onlying.cn
 * @date 2018/7/10 20:52
 */
@Component
public class MySpringTask {

    private Logger logger = LoggerFactory.getLogger(MySpringTask.class);

    /**
     * spring task,@Scheduled(cron = "0/5 * * * * ? ")即可开启定时任务
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void doTask(){
        logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "  "+ Thread.currentThread().getName());
    }
}

基于配置文件

第一步

创建applicationContext-task.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"
       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.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描组件 -->
    <context:component-scan base-package="top.simba1949.task" />
    <!-- 配置调度器,声明十个线程的池 -->
    <task:scheduler id="myScheduler" pool-size="10"/>
    <!-- 配置任务 -->
    <task:scheduled-tasks scheduler="myScheduler">
        <!--ref引用的定时任务的Bean,method指定该bean要执行的方法-->
        <task:scheduled ref="mySpringTask" method="doTask" cron="0/5 * * * * ? "/>
        <!--配置定时任务2-->
        <task:scheduled ref="mySpringTask02" method="doTask" cron="0/3 * * * * ? "/>
    </task:scheduled-tasks>

</beans>

第二步

将配置文件加入spring容器管理

package top.simba1949.config;

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

/**
 * @author simba@onlying.cn
 * @date 2018/7/9 20:09
 */
@Configuration
@ImportResource(locations = "applicationContext-springtask.xml")
public class SpringTaskConfig {

}

第三步

编写定时任务

package top.simba1949.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * @author simba@onlying.cn
 * @date 2018/7/10 20:52
 */
@Component
public class MySpringTask {

    private Logger logger = LoggerFactory.getLogger(MySpringTask.class);

    /**
     * spring task,@Scheduled(cron = "0/5 * * * * ? ")即可开启定时任务
     */
    public void doTask(){
        logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "  "+ Thread.currentThread().getName());
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simba1949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值