Skip to content

Commit

Permalink
v3.2.0 新增方法;自动重置开始时间戳
Browse files Browse the repository at this point in the history
  • Loading branch information
ALI1416 committed Feb 29, 2024
1 parent 248bae6 commit cbbcc32
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 更新日志

## v3.2.0 `2023.02.29`

- 新增方法
- 自动重置开始时间戳

## v3.1.1 `2023.12.26`

- 新增方法
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<dependency>
<groupId>cn.404z</groupId>
<artifactId>id</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
<version>1.5.0</version>
</dependency>
```

Expand Down Expand Up @@ -139,12 +139,12 @@ for (int i = 0; i < 60; i++) {
}
// INFO cn.z.id.Id -- 高性能雪花ID生成器预初始化:机器码MACHINE_ID 0 ,机器码位数MACHINE_BITS 8 ,序列号位数SEQUENCE_BITS 12 ,最大机器码MACHINE_ID 255 ;1ms最多生成ID 4096 个,起始时间 2021-01-01 08:00:00.0 ,失效时间 2299-09-27 23:10:22.207 ,大约可使用 278 年
// INFO cn.z.id.IdTest -- ID 90442426204815360
// INFO cn.z.id.Id -- 重置开始时间戳,时钟总共回拨 0 毫秒
// INFO cn.z.id.IdTest -- 总共回拨时间为:0毫秒
// INFO cn.z.id.Id -- 重置开始时间戳,时钟正拨 0 毫秒
// INFO cn.z.id.IdTest -- 时钟正拨时间为:0毫秒
// WARN cn.z.id.Id -- 监测到系统时钟发生了回拨。回拨时间 2023-09-26 14:03:30.023 ,上一个生成的时间 2023-09-26 15:03:38.985
// INFO cn.z.id.IdTest -- ID 90442426205863936
// INFO cn.z.id.Id -- 重置开始时间戳,时钟总共回拨 3608963 毫秒
// INFO cn.z.id.IdTest -- 总共回拨时间为:3608963毫秒
// INFO cn.z.id.Id -- 重置开始时间戳,时钟正拨 3608963 毫秒
// INFO cn.z.id.IdTest -- 时钟正拨时间为:3608963毫秒
```

## 工具
Expand Down Expand Up @@ -184,7 +184,7 @@ log.info(String.valueOf(Id.format(parse2[1], 8L, 12L, parse2[0], parse2[2])));
// INFO cn.z.id.IdTest -- 44161594381921

/* 获取id的时间戳 */
log.info(String.valueOf(new Timestamp(Id.timestamp(id))));
log.info(String.valueOf(Id.newTimestamp(id)));
// INFO cn.z.id.IdTest -- 2023-12-23 15:13:04.144
```

Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>id</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>

<parent>
<groupId>cn.404z</groupId>
Expand All @@ -31,20 +31,20 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
<version>1.5.0</version>
<scope>provided</scope>
</dependency>
<!-- 仅供测试使用 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<version>1.18.30</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
72 changes: 71 additions & 1 deletion src/main/java/cn/z/id/Id.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
* <h1>高性能雪花ID生成器</h1>
Expand Down Expand Up @@ -102,6 +104,44 @@ public class Id {

static {
initInner("预初始化", 0, 8, 12);
autoReset();
}

/**
* 自动重置开始时间戳
*
* @since 3.2.0
*/
private static void autoReset() {
// 每10分钟检查一次
Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "IdAutoReset");
thread.setDaemon(true);
return thread;
}).scheduleAtFixedRate(() -> {
// 未发生时钟回拨
if (startTimestamp == INITIAL_TIMESTAMP) {
return;
}
// 已回拨毫秒数
long difference = INITIAL_TIMESTAMP - startTimestamp;
long currentTimestamp = Clock.now();
// 最大回拨毫秒数
long maxDifference = currentTimestamp - lastTimestamp;
if (maxDifference >= difference) {
// 修改"开始时间戳"为"初始时间戳"
startTimestamp = INITIAL_TIMESTAMP;
log.warn("自动重置开始时间戳,时钟正拨 {} 毫秒,不需再正拨", difference);
} else {
// 修改"开始时间戳"为"开始时间戳"+最大回拨毫秒数
startTimestamp += maxDifference;
log.warn("自动重置开始时间戳,时钟正拨 {} 毫秒,还需正拨 {} 毫秒", maxDifference, difference - maxDifference);
}
// "序列号"归零
sequence = getRandomInitSequence();
// 更新"上一个时间戳"为"当前时间戳"
lastTimestamp = currentTimestamp;
}, 10, 10, TimeUnit.MINUTES);
}

private Id() {
Expand Down Expand Up @@ -225,9 +265,15 @@ public static synchronized long next() {
* @since 2.3.0
*/
public static long reset() {
// 已回拨毫秒数
long difference = INITIAL_TIMESTAMP - startTimestamp;
// 修改"开始时间戳"为"初始时间戳"
startTimestamp = INITIAL_TIMESTAMP;
log.info("重置开始时间戳,时钟总共回拨 {} 毫秒", difference);
log.info("重置开始时间戳,时钟正拨 {} 毫秒", difference);
// "序列号"归零
sequence = getRandomInitSequence();
// 更新"上一个时间戳"为"当前时间戳"
lastTimestamp = Clock.now();
return difference;
}

Expand Down Expand Up @@ -364,4 +410,28 @@ public static long timestamp(long id) {
return timestamp(MACHINE_BITS, SEQUENCE_BITS, id);
}

/**
* 获取id的Timestamp
*
* @param machineBits 机器码位数
* @param sequenceBits 序列号位数
* @param id id
* @return Timestamp
* @since 3.2.0
*/
public static Timestamp newTimestamp(long machineBits, long sequenceBits, long id) {
return new Timestamp(timestamp(machineBits, sequenceBits, id));
}

/**
* 根据配置参数获取id的Timestamp
*
* @param id id
* @return Timestamp
* @since 3.2.0
*/
public static Timestamp newTimestamp(long id) {
return new Timestamp(timestamp(id));
}

}
18 changes: 9 additions & 9 deletions src/test/java/cn/z/id/IdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ void test05Block() {
/**
* 时钟回拨(需要在1分钟内手动回拨时钟)
*/
// @Test
@Test
void test06Back() {
for (int i = 0; i < 60; i++) {
for (int i = 0; i < 600; i++) {
log.info("ID {}", Id.next());
try {
Thread.sleep(1000);
Thread.sleep(10000);
} catch (Exception ignore) {
Thread.currentThread().interrupt();
}
Expand All @@ -141,16 +141,16 @@ void test07Reset() {
} catch (Exception ignore) {
Thread.currentThread().interrupt();
}
log.info("总共回拨时间为:{}毫秒", Id.reset());
log.info("时钟正拨时间为:{}毫秒", Id.reset());
}
// INFO cn.z.id.Id -- 高性能雪花ID生成器预初始化:机器码MACHINE_ID 0 ,机器码位数MACHINE_BITS 8 ,序列号位数SEQUENCE_BITS 12 ,最大机器码MACHINE_ID 255 ;1ms最多生成ID 4096 个,起始时间 2021-01-01 08:00:00.0 ,失效时间 2299-09-27 23:10:22.207 ,大约可使用 278 年
// INFO cn.z.id.IdTest -- ID 90442426204815360
// INFO cn.z.id.Id -- 重置开始时间戳,时钟总共回拨 0 毫秒
// INFO cn.z.id.IdTest -- 总共回拨时间为:0毫秒
// INFO cn.z.id.Id -- 重置开始时间戳,时钟正拨 0 毫秒
// INFO cn.z.id.IdTest -- 时钟正拨时间为:0毫秒
// WARN cn.z.id.Id -- 监测到系统时钟发生了回拨。回拨时间 2023-09-26 14:03:30.023 ,上一个生成的时间 2023-09-26 15:03:38.985
// INFO cn.z.id.IdTest -- ID 90442426205863936
// INFO cn.z.id.Id -- 重置开始时间戳,时钟总共回拨 3608963 毫秒
// INFO cn.z.id.IdTest -- 总共回拨时间为:3608963毫秒
// INFO cn.z.id.Id -- 重置开始时间戳,时钟正拨 3608963 毫秒
// INFO cn.z.id.IdTest -- 时钟正拨时间为:3608963毫秒
}

/**
Expand Down Expand Up @@ -266,7 +266,7 @@ void test09Util() {
// INFO cn.z.id.IdTest -- 44161594381921

/* 获取id的时间戳 */
log.info(String.valueOf(new Timestamp(Id.timestamp(id))));
log.info(String.valueOf(Id.newTimestamp(id)));
// INFO cn.z.id.IdTest -- 2023-12-23 15:13:04.144
}

Expand Down

0 comments on commit cbbcc32

Please sign in to comment.