All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yanyun.cloud.SnowFlakeIdGenerator Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
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