SpringBoot整合定时任务

本文详细介绍如何在Spring框架中配置和使用定时任务,包括创建配置类实现SchedulingConfigurer接口,自定义ThreadPoolTaskScheduler参数,以及通过@Scheduled注解执行具体任务。示例展示了如何从Redis读取数据并更新数据库状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置文件

package com.sf.core;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author yachun
 * @date 2019/7/5
 */

@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setTaskScheduler(taskScheduler());
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        // 配置线程池大小,根据任务数量定制
        taskScheduler.setPoolSize(10);
        // 线程名称前缀
        taskScheduler.setThreadNamePrefix("spring-task-scheduler-thread-");
        // 线程池关闭前最大等待时间,确保最后一定关闭
        taskScheduler.setAwaitTerminationSeconds(60);
        // 线程池关闭时等待所有任务完成
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        // 任务丢弃策略
        taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return taskScheduler;
    }

}

执行任务代码

package com.sf.controller;

import com.sf.dao.mapper.BusinessCooperationMapper;
import com.sf.dao.model.BusinessCooperationDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yachun
 * @date 2019/7/5
 */
@Slf4j
@Component
public class BussinessTask {


	@Autowired
	private RedisTemplate redisTemplate;

	@Resource
	private BusinessCooperationMapper businessCooperationMapper;


	private static final String PT = "bussinessTask";

	/**
	 * 合作到期供应商合作修改审核状态
	 * TODO 需要改为@Scheduled(cron = "0 0 0 ? ? ?")
	 */
	@Scheduled(cron = "0 0 0 ? ? ?")
	public void pvTask() {

		log.info("----------------------------进入到我的BussinessTask定时器了-----------------");
		String bussinessTask = (String) redisTemplate.opsForValue().get("bussinessTask");
		if (!StringUtils.equals(PT, bussinessTask)) {
			// 需要把时间改为1分钟
			redisTemplate.opsForValue().set("bussinessTask", "1", 1, TimeUnit.MINUTES);

			List<Long> ids = businessCooperationMapper.listBuessIds();
			//执行修改供应商的审核状态为-- 2.合作到期
			if (ids != null && ids.size() > 0) {
				ids.stream().forEach(x -> {
					BusinessCooperationDO temp = new BusinessCooperationDO();
					temp.setId(x);
					temp.setAuditStatus(2);
					businessCooperationMapper.updateByPrimaryKeySelective(temp);
					log.info("----------------------------执行到修改供应商过期的审核状态的地方了-----------------");
				});

			}


		}


	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值