package org.example.task; import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.example.common.MarketDataClient; import org.example.pojo.Currency; import org.example.pojo.ExchangeInfo; import org.example.util.JsonUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import static com.alibaba.druid.sql.ast.SQLPartitionValue.Operator.List; /** * @program: demo * @description: mexc交易所 * @create: 2024-07-15 17:22 **/ @Component @Slf4j public class MexcStock { private final AtomicBoolean syncCurrency = new AtomicBoolean(false); private final Lock syncCurrencyLock = new ReentrantLock(); /** * 同步mexc交易所交易对 */ @Scheduled(cron = "0 0/5 * * * ?") public void syncCurrency() { if (syncCurrency.get()) { return; } if (syncCurrencyLock.tryLock()) { System.out.println("【同步mexc交易所交易对】---->开市"); try { syncCurrency.set(true); sync(); } catch (Exception e) { System.err.println("【同步mexc交易所交易对】出现异常: " + e.getMessage()); } finally { syncCurrencyLock.unlock(); syncCurrency.set(false); System.out.println("【同步mexc交易所交易对】---->结束"); } } } public void sync() { HashMap symbolParams = Maps.newHashMap(ImmutableMap.builder() .build()); String json = JsonUtil.toJson(exchangeInfo(symbolParams)); Gson gson = new Gson(); Map map = gson.fromJson(json, Map.class); String symbols = JsonUtil.toJson(map.get("symbols")); ArrayList arrayList = gson.fromJson(symbols, ArrayList.class); } public static ExchangeInfo exchangeInfo(Map params) { return MarketDataClient.get("/api/v3/exchangeInfo", params, new TypeReference() { }); } }