当前位置: 首页 > news >正文

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

	@GetMapping("/startETL")
//	@Idempotent(expireTime = 90, info = "请勿90秒内连续点击")public R getGaugeTestData6() {log.info("start ETL");//redis设置t_data_load_record 值为2bladeRedis.set("t_data_load_record_type", 2);String s = atomicityService.startETL();return R.data(s);}
@Slf4j
@Service
public class AtomicityService {@Autowiredprivate 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 "任务已经在执行中,本次请求被驳回";}}

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

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@Resourceprivate AtomicityService atomicityService;@Resourceprivate 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("定时任务执行测试");
//	}
}

http://www.lryc.cn/news/311426.html

相关文章:

  • golang常见面试题
  • 探索Python编程世界:从入门到精通
  • Spark Shuffle Tracking 原理分析
  • Docker 干货系列 (持续更新)
  • 一.jwt token 前后端的逻辑
  • day12_oop_抽象和接口
  • linux 将 api_key设置环境变量里
  • java八股文复习-----2024/03/03
  • UE4 Niagara 关卡3.4官方案例解析
  • C# Onnx segment-anything 分割万物 一键抠图
  • Linux配置网卡功能
  • 【C++】十大排序算法之 归并排序 快速排序
  • x-pack的破解方式和免费jar包!!可直接用!!
  • 最新版本,Midjourney保姆级教程!
  • Android中的几种定位方式调用详解
  • 【软件测试】接口调不通排查分析+常遇面试题总结
  • c++基础学习第三天(指针,结构体)
  • 【数仓】zookeeper软件安装及集群配置
  • Qt 实现橡皮擦拭显示图片
  • Vue3+Element-Plus中ELMessage样式丢失处理
  • 97 spring 中的泛型类型注入
  • C++设计模式
  • 反向代购业务系统|无货源代购中国商品|反向海淘代购系统
  • Linux 进程间通信
  • hippy 调试demo运行联调-mac环境准备篇
  • 【golang】go module依赖的git tag被覆盖 如何处理 | 因测试产生大量的git tag 如何清除 最佳实践
  • Spring Cloud原理详解
  • 力扣76. 最小覆盖子串(滑动窗口)
  • 使用华为云云函数functiongraph
  • Android logcat系统