From 988feffcf2a66a5423294e2178a9087150ff9e42 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Mon, 2 Dec 2024 18:14:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=9B=AA=E8=8A=B1ID=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E5=8F=91=E7=94=9F=E6=97=B6=E9=97=B4=E5=9B=9E?= =?UTF-8?q?=E9=80=80=E6=97=B6=E4=B8=8D=E6=8B=92=E7=BB=9D=E7=94=9F=E6=88=90?= =?UTF-8?q?ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/id/SnowflakeIdGenerator.java | 9 ++--- .../web/id/SnowflakeIdGeneratorTest.java | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 hsweb-core/src/test/java/org/hswebframework/web/id/SnowflakeIdGeneratorTest.java diff --git a/hsweb-core/src/main/java/org/hswebframework/web/id/SnowflakeIdGenerator.java b/hsweb-core/src/main/java/org/hswebframework/web/id/SnowflakeIdGenerator.java index 278a09045..b0fcd6655 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/id/SnowflakeIdGenerator.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/id/SnowflakeIdGenerator.java @@ -51,7 +51,7 @@ public class SnowflakeIdGenerator { return create(ThreadLocalRandom.current().nextInt(31), ThreadLocalRandom.current().nextInt(31)); } - private SnowflakeIdGenerator(long workerId, long dataCenterId) { + public SnowflakeIdGenerator(long workerId, long dataCenterId) { // sanity check for workerId if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); @@ -66,10 +66,11 @@ public class SnowflakeIdGenerator { public synchronized long nextId() { long timestamp = timeGen(); - + //时间回退 if (timestamp < lastTimestamp) { - log.error("clock is moving backwards. Rejecting requests until {}.", lastTimestamp); - throw new UnsupportedOperationException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + //发生回退时不拒绝,有可能出现重复数据? + log.warn("clock is moving backwards {}.", lastTimestamp); +// throw new UnsupportedOperationException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { diff --git a/hsweb-core/src/test/java/org/hswebframework/web/id/SnowflakeIdGeneratorTest.java b/hsweb-core/src/test/java/org/hswebframework/web/id/SnowflakeIdGeneratorTest.java new file mode 100644 index 000000000..9ef490c4a --- /dev/null +++ b/hsweb-core/src/test/java/org/hswebframework/web/id/SnowflakeIdGeneratorTest.java @@ -0,0 +1,35 @@ +package org.hswebframework.web.id; + +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicLong; + +import static org.junit.Assert.*; + +public class SnowflakeIdGeneratorTest { + + + @Test + public void test(){ + + AtomicLong time = new AtomicLong(System.currentTimeMillis()); + + + SnowflakeIdGenerator generator = new SnowflakeIdGenerator(0,1){ + @Override + protected long timeGen() { + return time.get(); + } + }; + + System.out.println(generator.nextId()); + //回退1秒 + time.addAndGet(-1000); + System.out.println(generator.nextId()); + + time.addAndGet(2000); + System.out.println(generator.nextId()); + + } + +} \ No newline at end of file