基于springboot的Java快速定时任务

一、常用的定时任务管理XXL-JOB,但是如果快速使用定时任务使用

复制代码
ApplicationRunner也很快,不要引入第三方包

示例代码:

java 复制代码
import cn.ctg.bfws.client.CtgConfigureClient;
import cn.ctg.bfws.vo.PmApproveHourVo;
import cn.ctg.common.response.ResponseData;
import cn.ctg.configure.dto.SysProjectDto;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 线程服务类
 * 
 * @Description:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ToolServiceThread implements ApplicationRunner {
    @Resource
	private CtgConfigureClient ctgConfigureClient;

	@Resource
	private PmApproveService pmApproveService;

	@Value("${bill.projectGs.billTempId:}")
	private String billTempId;
	@Value("${bill.projectGs.comTempCode:}")
	private String comTempCode;

	@Value("${bill.projectGs.categoryId:}")
	private String categoryId;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		log.info("开启线程.....,");
	}

		/**
		 * 每5分钟后执行
		 */
		@Scheduled(cron = "0 */5 * * * ?")
		public void executeEvery30Seconds () throws Exception {
			//自动拉取成员单,更新项目经理工时单
			log.info("更新项目经理工时单...");
			ResponseData<List<SysProjectDto>>  res = ctgConfigureClient.selectList();
			YearMonth yearMonthObj = YearMonth.now();
			String yearMonth = yearMonthObj.format(DateTimeFormatter.ofPattern("yyyyMM"));
			if(res.isSuccess()){
				List<SysProjectDto> list = res.getData();
				for (SysProjectDto sysProjectDto : list) {
					PmApproveHourVo vo  = new  PmApproveHourVo();
					vo.setMonth(yearMonth);
					vo.setProjectCode(sysProjectDto.getCode());
					String  userId =sysProjectDto.getManagers();
					if(sysProjectDto.getManagers().contains("^")) {
						userId = sysProjectDto.getManagers().split("^")[0];
					}
					pmApproveService.waitApproveList(vo,userId);
					log.info("更新项目经理工时单...:{}", JSON.toJSONString(sysProjectDto));
				}
			}

		}


	/**
	 * 每月5号生成项目经理公式模版示例
	 */
	@Scheduled(cron = "0 0 0 5 * ?")
	public void createPmGsBill () throws Exception {
		//自动拉取成员单,更新项目经理工时单
		log.info("每月5号生成项目经理工时模版...");
		ResponseData<List<SysProjectDto>>  res = ctgConfigureClient.selectList();
		YearMonth yearMonthObj = YearMonth.now();
		String yearMonth = yearMonthObj.format(DateTimeFormatter.ofPattern("yyyyMM"));
		//pmApproveService.createProjectBill(billTempId,comTempCode,categoryId);

	}

}

注意的是需要在启动类上加上注解

复制代码
@EnableScheduling 要不然定时任务是无效的

示例:

相关推荐
有个傻瓜5 小时前
分页 total 对不上、列表还少几条:MyBatis-Plus 一对多 LEFT JOIN 分页错乱复盘与四种写法实测
spring boot·sql优化·mybatis-plus·分页查询·踩坑复盘
考虑考虑5 小时前
JDK27结构化并发
java·后端·java ee
宝桥南山6 小时前
Visual Studio - 记录一个Visual Studio 2026(Insiders)的更新问题
windows·microsoft·微软·.net·.netcore·visual studio
Cosolar8 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
JosieBook11 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
千寻技术帮16 小时前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
会编程的吕洞宾17 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤17 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha17 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan18 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读