From 575cdb2fe13b1308a724657bdf922cd15d945ad9 Mon Sep 17 00:00:00 2001 From: 18796357645 <674126018@qq.com> Date: Tue, 13 May 2025 22:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/main/java/io/FrontApplication.java | 2 + .../main/java/io/config/MyScheduledTask.java | 87 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 front/src/main/java/io/config/MyScheduledTask.java diff --git a/front/src/main/java/io/FrontApplication.java b/front/src/main/java/io/FrontApplication.java index 037cf9d..739b3ba 100644 --- a/front/src/main/java/io/FrontApplication.java +++ b/front/src/main/java/io/FrontApplication.java @@ -4,11 +4,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.scheduling.annotation.EnableScheduling; /** * front */ @SpringBootApplication +@EnableScheduling public class FrontApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(FrontApplication.class, args); diff --git a/front/src/main/java/io/config/MyScheduledTask.java b/front/src/main/java/io/config/MyScheduledTask.java new file mode 100644 index 0000000..29e12cc --- /dev/null +++ b/front/src/main/java/io/config/MyScheduledTask.java @@ -0,0 +1,87 @@ +package io.config; + +import io.modules.item.dao.CaseDao; +import io.modules.item.entity.CaseEntity; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.hibernate.validator.internal.util.Contracts; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import java.security.SecureRandom; +import java.util.HexFormat; +import java.util.List; + +/** + * + * 1.在主方法加@EnableScheduling + */ + +@Component +@Slf4j +public class MyScheduledTask { + + + @Autowired + CaseDao contractsService; + + // 每5秒执行一次 + @Scheduled(fixedRate = 5000) + public void runEveryFiveSeconds() { + //需要检测的hex字段 + List list = contractsService.selectList(null); + for (CaseEntity itemEntity : list) { + if (!isValidEthereumHexId(itemEntity.getHex(),64)){ + itemEntity.setHex(sendSetRequest(itemEntity.getId().toString(),itemEntity.toString())); + //更新数据 + contractsService.updateById(itemEntity); + } + } + } + public static String sendSetRequest(String key, String value) { + try { + RestTemplate restTemplate = new RestTemplate(); + String url = "http://localhost:8080/set?key=" + key + "&value=" + value; + // 发送 GET 请求并解析返回 JSON 为 SetResponse 对象 + ResponseEntity response = restTemplate.getForEntity(url, SetResponse.class); + return response.getBody().data; + }catch (Exception e){ + SetResponse setRequestService = new SetResponse(); + setRequestService.setData(generate(40)); + return setRequestService.data; + } + } + /** + * 判断是否为合法的以太坊Hex ID(例如:交易哈希、区块哈希、地址) + * @param hexId 要校验的字符串 + * @param length 字符长度(40 = 钱包地址,64 = 交易哈希/区块哈希) + * @return true 表示合法,否则 false + */ + public static boolean isValidEthereumHexId(String hexId, int length) { + if (hexId == null || !hexId.startsWith("0x")) { + return false; + } + String hexBody = hexId.substring(2); + String pattern = "^[0-9a-fA-F]{" + length + "}$"; + return hexBody.matches(pattern); + } + + + // 内嵌的SetResponse类 + @Data + public static class SetResponse { + private String msg; + private String data; + } + + private static final SecureRandom secureRandom = new SecureRandom(); + private static final HexFormat hexFormat = HexFormat.of(); + + public static String generate(int byteLength) { + byte[] randomBytes = new byte[byteLength]; + secureRandom.nextBytes(randomBytes); + return "0x" + hexFormat.formatHex(randomBytes); + } +} \ No newline at end of file