SpringBoot搭建多线程定时任务

SpringBoot搭建多线程定时任务


  在一些系统上线之前,总需要后台定时执行一些事情。这个时候就需要定时任务来做这件事情。
  多线程的目的是:使同一个定时任务模块的两个不同的定时任务业务不互相影响。在SpringBoot中集成了定时任务的简单配置

package com.resource.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @program: travel-res-service
 * @description: 定时任务
 * @author: Andrea
 * @create: 2019-11-15 14:54
 **/
//@Component
@Configuration//标记启动类,兼备Component效果
@EnableScheduling//开启定时任务
@EnableAsync//多线程标记(两个定时任务不互相影响)
public class CornJob {

    //或直接指定时间间隔,例如:5秒
    //@Scheduled(fixedRate=5000)
    //3.添加定时任务
    @Async
    @Scheduled(cron = "0 50 2 * * ?")//每天2:50分执行
    public void cleanSegDatas() {
        boolean b = //定时执行业务1的返回结果
        if (b){
            System.out.println("执行业务1完成");
        }
        System.err.println("执行执行业务1定时任务时间: " + LocalDateTime.now());
    }
    @Async
    @Scheduled(cron = "0 0 3 * * ?")//每天3:00分执行
    public void cleanTraRouteDatas() {
        boolean b = //定时执行业务2的返回结果
        if (b){
            System.out.println("业务2完成");
        }
        System.err.println("执行业务2定时任务时间: " + LocalDateTime.now());
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值