KKSU
2024-06-06 424ad35dcd1fafb51a0ba77523b11e6871f2858c
src/main/java/cc/mrbird/febs/job/ChainListenerJob.java
@@ -7,6 +7,8 @@
import cc.mrbird.febs.dapp.chain.ContractEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
@@ -18,52 +20,18 @@
@Slf4j
@Component
@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true")
public class ChainListenerJob {
public class ChainListenerJob implements ApplicationRunner {
    @Autowired
    private ContractEventService bscUsdtContractEvent;
    @Autowired
    private ContractEventService bscCoinContractEvent;
    @Autowired
    private ContractEventService bscUsdtContractEvent;
    @Autowired
    private RedisUtils redisUtils;
    @PostConstruct
    public void chainListenerJob() {
        long start = System.currentTimeMillis();
        log.info("区块链监听开始启动");
        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
        BigInteger newest = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
        BigInteger block;
        if (incrementObj == null) {
            block = newest;
        } else {
            block = (BigInteger) incrementObj;
        }
        BigInteger section = BigInteger.valueOf(5000);
        while (newest.subtract(block).compareTo(section) > -1) {
            BigInteger end = block.add(section);
            log.info("监听:[{} - {}]", block, end);
            ChainService.contractEventListener(block, end, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
            ChainService.contractEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
            block = block.add(section);
            if (block.compareTo(newest) > 0) {
                block = newest;
            }
        }
        ChainService.contractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
        ChainService.contractEventListener(block, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
        long end = System.currentTimeMillis();
        log.info("区块链监听启动完成, 消耗时间{}", end - start);
    }
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void chainBlockUpdate() {
        log.info("最新区块更新");
        BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
        redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber);
@@ -96,4 +64,42 @@
        redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM, toIncrement);
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        long start = System.currentTimeMillis();
        log.info("区块链监听开始启动");
        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
        BigInteger newest = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
        BigInteger block;
        if (incrementObj == null) {
            block = newest;
        } else {
            block = (BigInteger) incrementObj;
        }
        ChainService.wssContractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT_LISTENER.name());
        ChainService.wssContractEventListener(block, bscCoinContractEvent, ChainEnum.BSC_TFC_LISTENER.name());
//        BigInteger section = BigInteger.valueOf(5000);
//        while (newest.subtract(block).compareTo(section) > -1) {
//            BigInteger end = block.add(section);
//
//            BigInteger finalBlock = block;
//            new Thread(() -> {
//                log.info("监听:[{} - {}]", finalBlock, end);
//                ChainService.contractEventListener(finalBlock, end, bscUsdtContractEvent, ChainEnum.BSC_USDT_LISTENER.name());
//                ChainService.contractEventListener(finalBlock, end, bscCoinContractEvent, ChainEnum.BSC_TFC_LISTENER.name());
//            }).start();
//
//            block = block.add(section);
//            if (block.compareTo(newest) > 0) {
//                block = newest;
//            }
//        }
//        ChainService.contractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT_LISTENER.name());
//        ChainService.contractEventListener(block, bscCoinContractEvent, ChainEnum.BSC_TFC_LISTENER.name());
        long end = System.currentTimeMillis();
        log.info("区块链监听启动完成, 消耗时间{}", end - start);
    }
}