From a8473491fd6ed93fe338dc8ab5297396e3c922d3 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Mon, 08 Aug 2022 11:46:31 +0800 Subject: [PATCH] fix --- src/main/java/cc/mrbird/febs/job/ChainListenerJob.java | 79 ++++++++++++++++++++------------------- 1 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java b/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java index a12e001..696cbcc 100644 --- a/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java +++ b/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java @@ -7,6 +7,9 @@ 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; import org.springframework.stereotype.Component; @@ -16,7 +19,8 @@ @Slf4j @Component -public class ChainListenerJob { +@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true") +public class ChainListenerJob implements ApplicationRunner { @Autowired private ContractEventService bscCoinContractEvent; @@ -27,8 +31,42 @@ @Autowired private RedisUtils redisUtils; - @PostConstruct - public void chainListenerJob() { + @Scheduled(cron = "0 0/5 * * * ? ") + public void chainBlockUpdate() { + BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber(); + + redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber); + } + + @Scheduled(cron = "0/2 * * * * ? ") + public void chainIncrementBlock() { + Object newestBlockObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM); + BigInteger newestBlock; + if (newestBlockObj == null) { + newestBlock = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber(); + } else { + newestBlock = (BigInteger) newestBlockObj; + } + + Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM); + BigInteger toIncrement; + if (incrementObj == null) { + toIncrement = newestBlock; + } else { + BigInteger incrementBlock = (BigInteger) incrementObj; + + // 最新区块小于增加区块 + if (newestBlock.compareTo(incrementBlock) <= 0) { + return; + } + toIncrement = incrementBlock.add(BigInteger.ONE); + } + + 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); @@ -58,39 +96,4 @@ long end = System.currentTimeMillis(); log.info("区块链监听启动完成, 消耗时间{}", end - start); } - - @Scheduled(cron = "0 0/5 * * * ? ") - public void chainBlockUpdate() { - BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber(); - - redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber); - } - - @Scheduled(cron = "0/2 * * * * ? ") - public void chainIncrementBlock() { - Object newestBlockObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM); - BigInteger newestBlock; - if (newestBlockObj == null) { - newestBlock = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber(); - } else { - newestBlock = (BigInteger) newestBlockObj; - } - - Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM); - BigInteger toIncrement; - if (incrementObj == null) { - toIncrement = newestBlock; - } else { - BigInteger incrementBlock = (BigInteger) incrementObj; - - // 最新区块小于增加区块 - if (newestBlock.compareTo(incrementBlock) < 0) { - return; - } - toIncrement = incrementBlock.add(BigInteger.ONE); - } - - redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM, toIncrement); - } - } -- Gitblit v1.9.1