如何保证某个程序系统内只运行一个,保证原子性

java 复制代码
	@GetMapping("/startETL")
//	@Idempotent(expireTime = 90, info = "请勿90秒内连续点击")
	public R getGaugeTestData6() {
		log.info("start ETL");
		//redis设置t_data_load_record 值为2
		bladeRedis.set("t_data_load_record_type", 2);
		String s = atomicityService.startETL();
		return R.data(s);
	}
java 复制代码
@Slf4j
@Service
public class AtomicityService {


	@Autowired
	private DataSourceService dataSourceService;


	// 使用 AtomicBoolean 作为互斥标志
	private final AtomicBoolean isExecuting = new AtomicBoolean(false);
	private final AtomicBoolean isExecutingForXRRJ = new AtomicBoolean(false);

	public String startETL() {
		if (isExecuting.compareAndSet(false, true)) {
			try {
				long startTime = System.currentTimeMillis(); // 记录开始时间
				ETLExecutionThreadLocal.setStartTime(startTime);
				// 执行 ETL 任务的具体逻辑
				//查询已经存在的数据的 批次号和站点号 做排除用
				List batchSiteList = dataSourceService.ReadBatchSiteList();
				List<Long> batchList = dataSourceService.ReadBatchList();
				List<Long> testerList = null;//测试人还是全删除吧,不要做增量查询,否则有些人被更新过信息进不来
				//读取数据源
				dataSourceService.ReadDataSource(batchSiteList, batchList, testerList);
				long endTime = System.currentTimeMillis();
				long executionTimeSeconds = (endTime - startTime) / 1000; // 计算用时(秒)
				return "ETL执行成功,用时:" + executionTimeSeconds + "秒";
			} finally {
				isExecuting.set(false);
			}
		} else {
			// 如果定时任务执行期间有手动执行请求,则驳回
			log.info("任务已经在执行中,本次请求被驳回");
			return "任务已经在执行中,本次请求被驳回";
		}
	}

定时任务在此与其共用一个程序,两者不可同时执行

java 复制代码
package org.springblade.etl.source.task;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.etl.source.service.AtomicityService;
import org.springblade.etl.source.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class ETLJob {

	// 注入 dataSourceService
	@Resource
	private AtomicityService atomicityService;

	@Resource
	private BladeRedis bladeRedis;
	@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行一次
	public void executeETL() {
		// 执行 startETL 方法
		log.info("Scheduled ETL job started");
		bladeRedis.set("t_data_load_record_type", 1);
		R result = getGaugeTestData6();
		log.info("ETL job finished with result: " + result);
	}

	// 在此处定义 getGaugeTestData6 方法的具体实现
	public R getGaugeTestData6() {
		log.info("start for Scheduled ETL job");
		atomicityService.startETL();
		return R.data("success for Scheduled ETL job");
	}


//	@Scheduled(cron = "0/1 * * * * ?") // 每秒执行一次
//	public void executeTestJob() {
//		log.info("定时任务执行测试");
//	}
}
相关推荐
冒险的豆子5 天前
JS 原型链 通俗易懂 解释说明(类比 关系)
开发语言·javascript·原型模式
吠品13 天前
箭头函数为什么不能 new
开发语言·javascript·原型模式
2603_9658966218 天前
JS原型与原型链|对象继承与实例底层原理
开发语言·javascript·原型模式
YYYing.21 天前
【设计模式系列 (五) 】原型模式
开发语言·后端·设计模式·原型模式·c/c++
羞儿1 个月前
【读点论文】Overall positive prototype for few-shot open-set recognition
原型模式·小样本学习·特征优化·开集学习
liangshanbo12151 个月前
手写 Function.prototype.call 与 Function.prototype.apply 面试题满分答案
开发语言·javascript·原型模式
江畔柳前堤1 个月前
云原生 × AI 全景图谱:从 Kubernetes 到 Agent 基础设施的一次认知跃迁
人工智能·分布式·gpt·云原生·kubernetes·原型模式·agi
艾伦野鸽ggg1 个月前
JavaScript 原型链与原型对象详解
javascript·原型模式
AgentMaster1 个月前
元数据、血缘、质量、安全四大模块能力拆解,数据治理方案对比:4 种技术路线深度评测
大数据·数据库·数据仓库·人工智能·原型模式
江畔柳前堤2 个月前
具身智能全景深度指南(2026年9月版):从“会聊天的AI“到“能干活的机器“
大数据·javascript·图像处理·人工智能·分布式·智慧城市·原型模式