This is a java version of Twitter Snowflake.
Twitter Snowflake is a network service for generating unique ID which requiring twitter's infrastructure service. We prefer Snowflake a simple java library rather than a network service so it can be embbed in any network service.
Snowflake snowflake = new Snowflake.Builder(serverId).build();
long id = snowflake.nextId();
The ID generated by Snowflake is composed of:
- 41bits for millisecond precision time (69 years)
- 10bits for a configured server identity (1024 server)
- 12 bits for a sequence number (4096 per server*milliseconds)
You can adjust the bits according to the actual situation.
Snowflake snowflake = new Snowflake.Builder(serverId, serverIdBits)
.setSequenceBits(sequenceBits)
.setEpoch(epoch)
.build();
long id = snowflake.nextId();