Package com.cedarsoftware.util
Class UniqueIdGenerator
- java.lang.Object
-
- com.cedarsoftware.util.UniqueIdGenerator
-
public class UniqueIdGenerator extends Object
Generate a unique ID that fits within a long value quickly, will never create a duplicate value, even if called insanely fast, and it incorporates part of the IP address so that machines in a cluster will not create duplicates. It guarantees no duplicates because it keeps the last 100 generated, and compares those against the value generated, if it matches, it will continue generating until it does not match. It will generate 100 per millisecond without matching. Once the requests for more than 100 unique IDs per millisecond is exceeded, the caller will be slowed down, because it will be retrying. Keep in mind, 100 per millisecond is 10 microseconds continuously without interruption.- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
getUniqueId()
static long
getUniqueId19()
ID format will be 1234567890123.9999.99 (no dots - only there for clarity - the number is a long).
-
-
-
Method Detail
-
getUniqueId
public static long getUniqueId()
-
getUniqueId19
public static long getUniqueId19()
ID format will be 1234567890123.9999.99 (no dots - only there for clarity - the number is a long). There are 13 digits for time - milliseconds since Jan 1, 1970. This is followed by a count that is 0000 through 9999. This is followed by a random 2 digit number. This number is chosen when the JVM is started and then stays fixed until next restart. This is to ensure cluster uniqueness. There is the possibility two machines could choose the same random number at start. Even still, collisions would be highly unlikely because for a collision to occur, a number would have to be chosen at the same millisecond, with the count at the same position. The returned ID will be 19 digits and this API will work through 2286. After then, it would likely return negative numbers (still unique). This API is faster than the 18 digit API. This API can return 10,000 unique IDs per millisecond max.- Returns:
- long unique ID
-
-