update ,解决同步问题可能造成的定时任务未按规定执行的问题

This commit is contained in:
yyb
2026-01-06 13:54:23 +08:00
parent 5559163b2e
commit b622701e39
5 changed files with 98 additions and 25 deletions

View File

@@ -2,11 +2,14 @@ package com.m2pool.lease.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@Configuration @Configuration
@EnableAsync
public class ThreadPoolConfig { public class ThreadPoolConfig {
@Bean(name = "customThreadPool") @Bean(name = "customThreadPool")
@@ -28,4 +31,48 @@ public class ThreadPoolConfig {
executor.initialize(); executor.initialize();
return executor; return executor;
} }
/**
* 定时任务专用线程池
* 用于解决多个定时任务同时执行时相互影响导致执行时间不固定的问题
*/
@Bean(name = "scheduledTaskExecutor")
public ThreadPoolTaskExecutor scheduledTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数,设置为定时任务的数量,确保每个任务都有独立的线程
executor.setCorePoolSize(10);
// 最大线程数
executor.setMaxPoolSize(20);
// 队列容量设置为0让任务直接进入线程池执行避免排队等待
executor.setQueueCapacity(0);
// 线程空闲时间(秒)
executor.setKeepAliveSeconds(60);
// 线程名前缀
executor.setThreadNamePrefix("scheduled-task-");
// 拒绝策略:由调用者线程执行(主线程),确保任务不会丢失
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 允许核心线程超时
executor.setAllowCoreThreadTimeOut(true);
// 初始化
executor.initialize();
return executor;
}
/**
* 异步任务执行器配置
*/
@Bean(name = "asyncTaskExecutor")
public AsyncTaskExecutor asyncTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(100);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("async-task-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(60);
executor.initialize();
return executor;
}
} }

View File

@@ -1,19 +1,19 @@
package com.m2pool.lease.task; package com.m2pool.lease.task;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.m2pool.lease.constant.BlockInterval;
import com.m2pool.lease.entity.LeaseGpuConfig; import com.m2pool.lease.entity.LeaseGpuConfig;
import com.m2pool.lease.entity.LeaseMiningSoftwareConfig; import com.m2pool.lease.entity.LeaseMiningSoftwareConfig;
import com.m2pool.lease.mapper.LeaseGpuConfigMapper; import com.m2pool.lease.mapper.LeaseGpuConfigMapper;
import com.m2pool.lease.mapper.LeaseMiningSoftwareConfigMapper; import com.m2pool.lease.mapper.LeaseMiningSoftwareConfigMapper;
import com.m2pool.lease.task.info.*; import com.m2pool.lease.task.info.AlgorithmInfo;
import io.lettuce.core.ScriptOutputType; import com.m2pool.lease.task.info.GpuInfo;
import org.springframework.context.annotation.Configuration; 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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@@ -33,6 +33,7 @@ import java.util.stream.Collectors;
*/ */
@Configuration @Configuration
@EnableScheduling @EnableScheduling
@EnableAsync
public class GpuRequestApiTask { public class GpuRequestApiTask {
@Resource @Resource
@@ -43,6 +44,7 @@ public class GpuRequestApiTask {
private LeaseMiningSoftwareConfigMapper leaseMiningSoftwareConfigMapper; private LeaseMiningSoftwareConfigMapper leaseMiningSoftwareConfigMapper;
@Scheduled(cron = "0 0 0/6 * * ? ") @Scheduled(cron = "0 0 0/6 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "0 0/5 * * * ? ") //@Scheduled(cron = "0 0/5 * * * ? ")
public void gpuInfoTask(){ public void gpuInfoTask(){
List<GpuInfo> gpuInfos = fetchGpuInfo(); List<GpuInfo> gpuInfos = fetchGpuInfo();

View File

@@ -21,6 +21,8 @@ import com.m2pool.lease.utils.DateUtils;
import com.m2pool.lease.utils.HashUtils; import com.m2pool.lease.utils.HashUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration; 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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -29,9 +31,7 @@ import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -46,6 +46,7 @@ import static com.m2pool.lease.constant.RabbitmqConstant.PAY_AUTO_QUEUE;
*/ */
@Configuration @Configuration
@EnableScheduling @EnableScheduling
@EnableAsync
public class OrderAndPayTask { public class OrderAndPayTask {
@@ -94,6 +95,7 @@ public class OrderAndPayTask {
*/ */
@Scheduled(cron = "0 5 1 * * ? ") @Scheduled(cron = "0 5 1 * * ? ")
//@Scheduled(cron = "0 0/1 * * * ? ") //@Scheduled(cron = "0 0/1 * * * ? ")
@Async("scheduledTaskExecutor")
@Transactional @Transactional
public void checkOperator(){ public void checkOperator(){
//1.查询lease_auto_address 表 状态为1 已使用 的钱包 //1.查询lease_auto_address 表 状态为1 已使用 的钱包
@@ -136,10 +138,12 @@ public class OrderAndPayTask {
} }
@Scheduled(cron = "0 0/1 * * * ? ") @Scheduled(cron = "0 0/2 * * * ? ")
@Async("scheduledTaskExecutor")
@DSTransactional @DSTransactional
public void paymentTaskV2(){ public void paymentTaskV2(){
System.out.println("支付开始"+LocalDateTime.now());
Date now = DateUtils.getPreviousHalfHourOrFullHour(new Date()); Date now = DateUtils.getPreviousHalfHourOrFullHour(new Date());
//开发环境 //开发环境
LocalDateTime now1 = LocalDateTime.now(); LocalDateTime now1 = LocalDateTime.now();
@@ -278,6 +282,7 @@ public class OrderAndPayTask {
if (!orderComplete.isEmpty()){ if (!orderComplete.isEmpty()){
handlerOrderAllComplete(orderComplete); handlerOrderAllComplete(orderComplete);
} }
System.out.println("支付结束"+LocalDateTime.now());
} }
@@ -298,16 +303,16 @@ public class OrderAndPayTask {
//LocalDateTime endTime = orderTimeInfoDto.getCreateTime().plusDays(orderTimeInfoDto.getLeaseTime()); //LocalDateTime endTime = orderTimeInfoDto.getCreateTime().plusDays(orderTimeInfoDto.getLeaseTime());
//TODO 开发环境测试用1分钟当一天 差值改为差1分钟 //TODO 开发环境测试用1分钟当一天 差值改为差1分钟
LocalDateTime endTime = orderTimeInfoDto.getCreateTime().plusMinutes(orderTimeInfoDto.getLeaseTime()); LocalDateTime endTime = orderTimeInfoDto.getCreateTime().plusMinutes(orderTimeInfoDto.getLeaseTime() * 2);
System.out.println("支付开始"+"开始时间"+now+ "结束时间"+endTime+ "差值:"+Duration.between(now, endTime).toMinutes());
// 获取订单完成的订单详情:比较 now 和 endTime 的年月日时分秒差值是否小于30分 // 获取订单完成的订单详情:比较 now 和 endTime 的年月日时分秒差值是否小于30分
if (Duration.between(now, endTime).toMinutes() <= 1) { if (Duration.between(now, endTime).toMinutes() <= 2) {
completeMachines.add(LeaseMachine.builder() completeMachines.add(LeaseMachine.builder()
.id(orderTimeInfoDto.getProductMachineId()) .id(orderTimeInfoDto.getProductMachineId())
.canSaleNumbers(orderTimeInfoDto.getNumbers()) .canSaleNumbers(orderTimeInfoDto.getNumbers())
.saleOutNumbers(orderTimeInfoDto.getNumbers()) .saleOutNumbers(orderTimeInfoDto.getNumbers())
.saleState(false) .saleState(false)
.build()); .build());
completeIds.add(orderTimeInfoDto.getId()); completeIds.add(orderTimeInfoDto.getId());
needPayIds.add(orderTimeInfoDto.getId()); needPayIds.add(orderTimeInfoDto.getId());
needPayInfos.add(orderTimeInfoDto); needPayInfos.add(orderTimeInfoDto);
@@ -812,6 +817,7 @@ public class OrderAndPayTask {
}); });
System.out.println("部分完成"+JSONUtil.toJsonPrettyStr(reocrdList));
leasePayRecordMessageService.saveBatch(reocrdList); leasePayRecordMessageService.saveBatch(reocrdList);
int buyerUpdate = leaseUserWalletDataMapper.updateBalanceAndBlockBalance(reocrdList); int buyerUpdate = leaseUserWalletDataMapper.updateBalanceAndBlockBalance(reocrdList);
int sellerUpdate = leaseShopConfigMapper.updateBalance(reocrdList); int sellerUpdate = leaseShopConfigMapper.updateBalance(reocrdList);
@@ -907,6 +913,7 @@ public class OrderAndPayTask {
/** /**
* 检测 临时表数据是否需要持久化到矿机表中 * 检测 临时表数据是否需要持久化到矿机表中
*/ */
@Async("scheduledTaskExecutor")
@Scheduled(cron = "0 0 0/1 * * ? ") @Scheduled(cron = "0 0 0/1 * * ? ")
@Transactional @Transactional
public void checkMachineTempTable(){ public void checkMachineTempTable(){

View File

@@ -1,29 +1,33 @@
package com.m2pool.lease.task; package com.m2pool.lease.task;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.m2pool.lease.dto.HourIncomeDto; import com.m2pool.lease.dto.HourIncomeDto;
import com.m2pool.lease.dto.OrderStatusDto; import com.m2pool.lease.dto.OrderStatusDto;
import com.m2pool.lease.entity.*; import com.m2pool.lease.entity.LeaseOrderInfo;
import com.m2pool.lease.mapper.*; import com.m2pool.lease.entity.LeaseOrderItem;
import com.m2pool.lease.entity.LeaseProductMachine;
import com.m2pool.lease.entity.LeaseUserOwnedProduct;
import com.m2pool.lease.mapper.LeaseOrderItemMapper;
import com.m2pool.lease.mapper.LeasePayRecordMessageMapper;
import com.m2pool.lease.mapper.LeaseProductMachineMapper;
import com.m2pool.lease.mapper.LeaseUserOwnedProductMapper;
import com.m2pool.lease.mq.message.RabbitmqPoolProxyMessage; import com.m2pool.lease.mq.message.RabbitmqPoolProxyMessage;
import com.m2pool.lease.service.LeaseOrderInfoService; import com.m2pool.lease.service.LeaseOrderInfoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration; 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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal;
import java.rmi.dgc.Lease;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.util.ArrayList;
import java.time.OffsetDateTime; import java.util.List;
import java.time.ZoneOffset; import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@@ -41,6 +45,7 @@ import static com.m2pool.lease.constant.RabbitmqConstant.POOL_PROXY_QUEUE_NAME;
*/ */
@Configuration @Configuration
@EnableScheduling @EnableScheduling
@EnableAsync
public class OwnProductTask { public class OwnProductTask {
@Resource @Resource
@@ -69,6 +74,7 @@ public class OwnProductTask {
* 修改已购表租约过期状态为1 已过期 + 机器表状态为0 未售出 + 修改租约到期订单状态为已完成 * 修改已购表租约过期状态为1 已过期 + 机器表状态为0 未售出 + 修改租约到期订单状态为已完成
*/ */
@Scheduled(cron = "0 1,5,7 0 * * ? ") @Scheduled(cron = "0 1,5,7 0 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "0 0/3 * * * ? ") //@Scheduled(cron = "0 0/3 * * * ? ")
@Transactional @Transactional
public void updateOwnMachineStateTask(){ public void updateOwnMachineStateTask(){
@@ -139,6 +145,7 @@ public class OwnProductTask {
@Scheduled(cron = "0 35 0/1 * * ? ") @Scheduled(cron = "0 35 0/1 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "30 0/2 * * * ? ") //@Scheduled(cron = "30 0/2 * * * ? ")
@DSTransactional @DSTransactional
public void updateNexaIncomeTask(){ public void updateNexaIncomeTask(){
@@ -149,6 +156,7 @@ public class OwnProductTask {
} }
@Scheduled(cron = "10 35 0/1 * * ? ") @Scheduled(cron = "10 35 0/1 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "30 0/2 * * * ? ") //@Scheduled(cron = "30 0/2 * * * ? ")
@DSTransactional @DSTransactional
public void updateGrsIncomeTask(){ public void updateGrsIncomeTask(){
@@ -158,6 +166,7 @@ public class OwnProductTask {
} }
@Scheduled(cron = "20 35 0/1 * * ? ") @Scheduled(cron = "20 35 0/1 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "30 0/2 * * * ? ") //@Scheduled(cron = "30 0/2 * * * ? ")
@DSTransactional @DSTransactional
public void updateRxdIncomeTask(){ public void updateRxdIncomeTask(){
@@ -167,6 +176,7 @@ public class OwnProductTask {
} }
@Scheduled(cron = "30 35 0/1 * * ? ") @Scheduled(cron = "30 35 0/1 * * ? ")
@Async("scheduledTaskExecutor")
//@Scheduled(cron = "30 0/2 * * * ? ") //@Scheduled(cron = "30 0/2 * * * ? ")
@DSTransactional @DSTransactional
public void updateMonaIncomeTask(){ public void updateMonaIncomeTask(){

View File

@@ -6,9 +6,11 @@ import com.m2pool.lease.dto.ProductMachineDto;
import com.m2pool.lease.entity.LeaseProductMachine; import com.m2pool.lease.entity.LeaseProductMachine;
import com.m2pool.lease.mapper.LeaseProductMachineMapper; import com.m2pool.lease.mapper.LeaseProductMachineMapper;
import org.springframework.context.annotation.Configuration; 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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -28,6 +30,7 @@ import java.util.concurrent.atomic.AtomicReference;
*/ */
@Configuration @Configuration
@EnableScheduling @EnableScheduling
@EnableAsync
public class RealPowerInsetTask { public class RealPowerInsetTask {
@Resource @Resource
@@ -85,6 +88,7 @@ public class RealPowerInsetTask {
@Scheduled(cron = "0 0/5 * * * ? ") @Scheduled(cron = "0 0/5 * * * ? ")
@Async("scheduledTaskExecutor")
@DSTransactional @DSTransactional
public void nexaRealPowerInset(){ public void nexaRealPowerInset(){
List<ProductMachineDto> nexaPower = getRealPower("nexa"); List<ProductMachineDto> nexaPower = getRealPower("nexa");
@@ -93,6 +97,7 @@ public class RealPowerInsetTask {
} }
@Scheduled(cron = "0 0/5 * * * ? ") @Scheduled(cron = "0 0/5 * * * ? ")
@Async("scheduledTaskExecutor")
@DSTransactional @DSTransactional
public void monaRealPowerInset(){ public void monaRealPowerInset(){
List<ProductMachineDto> monaPower = getRealPower("mona"); List<ProductMachineDto> monaPower = getRealPower("mona");
@@ -100,6 +105,7 @@ public class RealPowerInsetTask {
} }
@Scheduled(cron = "0 0/5 * * * ? ") @Scheduled(cron = "0 0/5 * * * ? ")
@Async("scheduledTaskExecutor")
@DSTransactional @DSTransactional
public void rxdRealPowerInset(){ public void rxdRealPowerInset(){
List<ProductMachineDto> rxdPower = getRealPower("mona"); List<ProductMachineDto> rxdPower = getRealPower("mona");
@@ -107,6 +113,7 @@ public class RealPowerInsetTask {
} }
@Scheduled(cron = "0 0/5 * * * ? ") @Scheduled(cron = "0 0/5 * * * ? ")
@Async("scheduledTaskExecutor")
@DSTransactional @DSTransactional
public void grsRealPowerInset(){ public void grsRealPowerInset(){
List<ProductMachineDto> rxdPower = getRealPower("grs"); List<ProductMachineDto> rxdPower = getRealPower("grs");