| | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.Future; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.locks.Lock; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | |
| | |
| | | private static final Logger log = LoggerFactory.getLogger(StockTask.class); |
| | | |
| | | |
| | | boolean syncINStockData = true; |
| | | |
| | | private final AtomicBoolean syncINStockData = new AtomicBoolean(false); |
| | | |
| | | private final Lock syncINStockDataLock = new ReentrantLock(); |
| | | |
| | | /** |
| | | * 同步系统所需要的股票 |
| | | */ |
| | | @Scheduled(cron = "0 0 0/2 * * ?") |
| | | @Scheduled(cron = "0 0/1 * * * ?") |
| | | public void syncINStockData() { |
| | | if (syncINStockData) // 定义一个布尔变量,代表新闻任务是否在处理中 |
| | | if (syncINStockData.get()) { // 判断任务是否在处理中 |
| | | return; |
| | | syncINStockData = true; |
| | | } |
| | | if (syncINStockDataLock.tryLock()) { |
| | | ExecutorService executor = Executors.newFixedThreadPool(2); |
| | | Future<?> usFuture = null; |
| | | Future<?> mxFuture = null; |
| | | try { |
| | | loadAllStock(EStockType.IN); |
| | | // loadAllStock(EStockType.HK); |
| | | syncINStockData.set(true); // 设置处理中标识为true |
| | | |
| | | // 并行执行US和MX的股票数据加载 |
| | | usFuture = executor.submit(() -> loadAllStock(EStockType.US)); |
| | | mxFuture = executor.submit(() -> loadAllStock(EStockType.MX)); |
| | | |
| | | // 等待两个任务都完成 |
| | | usFuture.get(); |
| | | mxFuture.get(); |
| | | } catch (Exception e) { |
| | | Thread.currentThread().interrupt(); |
| | | log.error("同步股票数据出错", e); |
| | | } finally { |
| | | // 关闭线程池 |
| | | if (executor != null) { |
| | | executor.shutdown(); |
| | | } |
| | | syncINStockDataLock.unlock(); |
| | | syncINStockData = false; |
| | | syncINStockData.set(false); // 设置处理中标识为false |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | boolean stockConstraint = true; |
| | | private final AtomicBoolean stockConstraint = new AtomicBoolean(false); |
| | | |
| | | /** |
| | | * 强制平仓 |
| | | */ |
| | | @Scheduled(cron = "0/1 * * * * ?") |
| | | // @Scheduled(cron = "0/1 * * * * ?") |
| | | public void stockConstraint() { |
| | | if (stockConstraint) // 定义一个布尔变量,代表新闻任务是否在处理中 |
| | | if (stockConstraint.get()) { // 判断任务是否在处理中 |
| | | return; |
| | | stockConstraint = true; |
| | | } |
| | | if (stockConstraintLock.tryLock()) { |
| | | try { |
| | | stockConstraint.set(true); // 设置处理中标识为true |
| | | List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId)); |
| | | if (CollectionUtils.isNotEmpty(userPositions)) { |
| | | userPositionService.stockConstraint(userPositions); |
| | |
| | | log.error("强制平仓任务错误:" + e.getMessage()); |
| | | } finally { |
| | | stockConstraintLock.unlock(); |
| | | stockConstraint = false; |
| | | stockConstraint.set(false); // 设置处理中标识为false |
| | | } |
| | | } else { |
| | | log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略"); |