update ,解决同步问题可能造成的定时任务未按规定执行的问题
This commit is contained in:
@@ -2,11 +2,14 @@ package com.m2pool.lease.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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 java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
public class ThreadPoolConfig {
|
||||
|
||||
@Bean(name = "customThreadPool")
|
||||
@@ -28,4 +31,48 @@ public class ThreadPoolConfig {
|
||||
executor.initialize();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
package com.m2pool.lease.task;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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.LeaseMiningSoftwareConfig;
|
||||
import com.m2pool.lease.mapper.LeaseGpuConfigMapper;
|
||||
import com.m2pool.lease.mapper.LeaseMiningSoftwareConfigMapper;
|
||||
import com.m2pool.lease.task.info.*;
|
||||
import io.lettuce.core.ScriptOutputType;
|
||||
import com.m2pool.lease.task.info.AlgorithmInfo;
|
||||
import com.m2pool.lease.task.info.GpuInfo;
|
||||
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.Scheduled;
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class GpuRequestApiTask {
|
||||
|
||||
@Resource
|
||||
@@ -43,6 +44,7 @@ public class GpuRequestApiTask {
|
||||
private LeaseMiningSoftwareConfigMapper leaseMiningSoftwareConfigMapper;
|
||||
|
||||
@Scheduled(cron = "0 0 0/6 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
public void gpuInfoTask(){
|
||||
List<GpuInfo> gpuInfos = fetchGpuInfo();
|
||||
|
||||
@@ -21,6 +21,8 @@ import com.m2pool.lease.utils.DateUtils;
|
||||
import com.m2pool.lease.utils.HashUtils;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
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.Scheduled;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -29,9 +31,7 @@ import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -46,6 +46,7 @@ import static com.m2pool.lease.constant.RabbitmqConstant.PAY_AUTO_QUEUE;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class OrderAndPayTask {
|
||||
|
||||
|
||||
@@ -94,6 +95,7 @@ public class OrderAndPayTask {
|
||||
*/
|
||||
@Scheduled(cron = "0 5 1 * * ? ")
|
||||
//@Scheduled(cron = "0 0/1 * * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
@Transactional
|
||||
public void checkOperator(){
|
||||
//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
|
||||
public void paymentTaskV2(){
|
||||
|
||||
System.out.println("支付开始"+LocalDateTime.now());
|
||||
Date now = DateUtils.getPreviousHalfHourOrFullHour(new Date());
|
||||
//开发环境
|
||||
LocalDateTime now1 = LocalDateTime.now();
|
||||
@@ -278,6 +282,7 @@ public class OrderAndPayTask {
|
||||
if (!orderComplete.isEmpty()){
|
||||
handlerOrderAllComplete(orderComplete);
|
||||
}
|
||||
System.out.println("支付结束"+LocalDateTime.now());
|
||||
}
|
||||
|
||||
|
||||
@@ -298,10 +303,10 @@ public class OrderAndPayTask {
|
||||
//LocalDateTime endTime = orderTimeInfoDto.getCreateTime().plusDays(orderTimeInfoDto.getLeaseTime());
|
||||
|
||||
//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分
|
||||
if (Duration.between(now, endTime).toMinutes() <= 1) {
|
||||
if (Duration.between(now, endTime).toMinutes() <= 2) {
|
||||
completeMachines.add(LeaseMachine.builder()
|
||||
.id(orderTimeInfoDto.getProductMachineId())
|
||||
.canSaleNumbers(orderTimeInfoDto.getNumbers())
|
||||
@@ -812,6 +817,7 @@ public class OrderAndPayTask {
|
||||
|
||||
|
||||
});
|
||||
System.out.println("部分完成"+JSONUtil.toJsonPrettyStr(reocrdList));
|
||||
leasePayRecordMessageService.saveBatch(reocrdList);
|
||||
int buyerUpdate = leaseUserWalletDataMapper.updateBalanceAndBlockBalance(reocrdList);
|
||||
int sellerUpdate = leaseShopConfigMapper.updateBalance(reocrdList);
|
||||
@@ -907,6 +913,7 @@ public class OrderAndPayTask {
|
||||
/**
|
||||
* 检测 临时表数据是否需要持久化到矿机表中
|
||||
*/
|
||||
@Async("scheduledTaskExecutor")
|
||||
@Scheduled(cron = "0 0 0/1 * * ? ")
|
||||
@Transactional
|
||||
public void checkMachineTempTable(){
|
||||
|
||||
@@ -1,29 +1,33 @@
|
||||
package com.m2pool.lease.task;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.m2pool.lease.dto.HourIncomeDto;
|
||||
import com.m2pool.lease.dto.OrderStatusDto;
|
||||
import com.m2pool.lease.entity.*;
|
||||
import com.m2pool.lease.mapper.*;
|
||||
import com.m2pool.lease.entity.LeaseOrderInfo;
|
||||
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.service.LeaseOrderInfoService;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
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.Scheduled;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.rmi.dgc.Lease;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -41,6 +45,7 @@ import static com.m2pool.lease.constant.RabbitmqConstant.POOL_PROXY_QUEUE_NAME;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class OwnProductTask {
|
||||
|
||||
@Resource
|
||||
@@ -69,6 +74,7 @@ public class OwnProductTask {
|
||||
* 修改已购表租约过期状态为1 已过期 + 机器表状态为0 未售出 + 修改租约到期订单状态为已完成
|
||||
*/
|
||||
@Scheduled(cron = "0 1,5,7 0 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "0 0/3 * * * ? ")
|
||||
@Transactional
|
||||
public void updateOwnMachineStateTask(){
|
||||
@@ -139,6 +145,7 @@ public class OwnProductTask {
|
||||
|
||||
|
||||
@Scheduled(cron = "0 35 0/1 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "30 0/2 * * * ? ")
|
||||
@DSTransactional
|
||||
public void updateNexaIncomeTask(){
|
||||
@@ -149,6 +156,7 @@ public class OwnProductTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "10 35 0/1 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "30 0/2 * * * ? ")
|
||||
@DSTransactional
|
||||
public void updateGrsIncomeTask(){
|
||||
@@ -158,6 +166,7 @@ public class OwnProductTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "20 35 0/1 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "30 0/2 * * * ? ")
|
||||
@DSTransactional
|
||||
public void updateRxdIncomeTask(){
|
||||
@@ -167,6 +176,7 @@ public class OwnProductTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "30 35 0/1 * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
//@Scheduled(cron = "30 0/2 * * * ? ")
|
||||
@DSTransactional
|
||||
public void updateMonaIncomeTask(){
|
||||
|
||||
@@ -6,9 +6,11 @@ import com.m2pool.lease.dto.ProductMachineDto;
|
||||
import com.m2pool.lease.entity.LeaseProductMachine;
|
||||
import com.m2pool.lease.mapper.LeaseProductMachineMapper;
|
||||
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.Scheduled;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
@@ -28,6 +30,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class RealPowerInsetTask {
|
||||
|
||||
@Resource
|
||||
@@ -85,6 +88,7 @@ public class RealPowerInsetTask {
|
||||
|
||||
|
||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
@DSTransactional
|
||||
public void nexaRealPowerInset(){
|
||||
List<ProductMachineDto> nexaPower = getRealPower("nexa");
|
||||
@@ -93,6 +97,7 @@ public class RealPowerInsetTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
@DSTransactional
|
||||
public void monaRealPowerInset(){
|
||||
List<ProductMachineDto> monaPower = getRealPower("mona");
|
||||
@@ -100,6 +105,7 @@ public class RealPowerInsetTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
@DSTransactional
|
||||
public void rxdRealPowerInset(){
|
||||
List<ProductMachineDto> rxdPower = getRealPower("mona");
|
||||
@@ -107,6 +113,7 @@ public class RealPowerInsetTask {
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
@Async("scheduledTaskExecutor")
|
||||
@DSTransactional
|
||||
public void grsRealPowerInset(){
|
||||
List<ProductMachineDto> rxdPower = getRealPower("grs");
|
||||
|
||||
Reference in New Issue
Block a user