com.yanyun.cloud.SnowFlakeIdGenerator Maven / Gradle / Ivy
package com.yanyun.cloud;
import com.twitter.snowflake.sequence.IdGenerator;
import com.twitter.snowflake.support.ElasticIdGeneratorFactory;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class SnowFlakeIdGenerator {
private final static IdGenerator elasticGenerator;
static {
ElasticIdGeneratorFactory elasticFactory = new ElasticIdGeneratorFactory();
elasticFactory.setTimeBits(41);
elasticFactory.setWorkerBits(10);
elasticFactory.setSeqBits(12);
elasticFactory.setTimeUnit(TimeUnit.MILLISECONDS);
elasticFactory.setEpochTimestamp(1483200000000L);
elasticGenerator = elasticFactory.create(1L);
}
public static String generateString() {
try {
return String.valueOf(elasticGenerator.nextId());
} catch (Exception e) {
e.printStackTrace();
return UUID.randomUUID().toString().replace("-", "");
}
}
public static long generateLong() {
return elasticGenerator.nextId();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy