| | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | 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) |
| | | syncINStockData = false; |
| | | else return; |
| | | if (syncINStockData.get()) { // 判断任务是否在处理中 |
| | | return; |
| | | } |
| | | if (syncINStockDataLock.tryLock()) { |
| | | try { |
| | | loadAllStock(EStockType.IN); |
| | | // loadAllStock(EStockType.HK); |
| | | syncINStockData.set(true); // 设置处理中标识为true |
| | | loadAllStock(EStockType.ST); |
| | | } finally { |
| | | syncINStockDataLock.unlock(); |
| | | syncINStockData = true; |
| | | syncINStockData.set(false); // 设置处理中标识为false |
| | | } |
| | | } |
| | | } |
| | |
| | | * 同步美国股票 |
| | | */ |
| | | // @Scheduled(cron = "0 0/30 * * * ?") |
| | | public void loadStockCompanies() { |
| | | loadAllCompanies(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加载公司信息 |
| | | */ |
| | | public void loadAllCompanies() { |
| | | List<Stock> list = stockMapper.findStockList(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | Stock stock = list.get(i); |
| | | EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType()); |
| | | String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey); |
| | | try { |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | | JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0); |
| | | RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo)); |
| | | } catch (Exception e) { |
| | | log.info(""); |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | // public void loadStockCompanies() { |
| | | // loadAllCompanies(); |
| | | // } |
| | | // |
| | | // |
| | | // /** |
| | | // * 加载公司信息 |
| | | // */ |
| | | // public void loadAllCompanies() { |
| | | // List<Stock> list = stockMapper.findStockList(); |
| | | // for (int i = 0; i < list.size(); i++) { |
| | | // Stock stock = list.get(i); |
| | | // EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType()); |
| | | // String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey); |
| | | // try { |
| | | // JSONObject jsonObject = JSONObject.parseObject(result); |
| | | // JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0); |
| | | // RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo)); |
| | | // } catch (Exception e) { |
| | | // log.info(""); |
| | | // |
| | | // } |
| | | // } |
| | | // |
| | | // } |
| | | |
| | | /** |
| | | * 加载所有股票数据 |
| | |
| | | } |
| | | } |
| | | |
| | | boolean stockConstraint = true; |
| | | private final AtomicBoolean stockConstraint = new AtomicBoolean(false); |
| | | |
| | | /** |
| | | * 强制平仓 |
| | | */ |
| | | @Scheduled(cron = "0/1 * * * * ?") |
| | | public void stockConstraint() { |
| | | if (stockConstraint) |
| | | stockConstraint = false; |
| | | else return; |
| | | if (stockConstraintLock.tryLock()) { |
| | | try { |
| | | List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId)); |
| | | if (CollectionUtils.isNotEmpty(userPositions)) { |
| | | userPositionService.stockConstraint(userPositions); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("强制平仓任务错误:" + e.getMessage()); |
| | | } finally { |
| | | stockConstraintLock.unlock(); |
| | | stockConstraint = true; |
| | | } |
| | | } else { |
| | | log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略"); |
| | | } |
| | | } |
| | | // @Scheduled(cron = "0/1 * * * * ?") |
| | | // public void stockConstraint() { |
| | | // if (stockConstraint.get()) { // 判断任务是否在处理中 |
| | | // return; |
| | | // } |
| | | // 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); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // log.error("强制平仓任务错误:" + e.getMessage()); |
| | | // } finally { |
| | | // stockConstraintLock.unlock(); |
| | | // stockConstraint.set(false); // 设置处理中标识为false |
| | | // } |
| | | // } else { |
| | | // log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略"); |
| | | // } |
| | | // } |
| | | } |