Package org.chenile.utils.entity.service
Class IDGenerator
java.lang.Object
org.chenile.utils.entity.service.IDGenerator
Generates IDs given a request context. This class is useful to generate predictable IDs for entities
given a request ID. This is essential for replicating systems i.e. systems that replicate the same
data in multiple machines by replaying the same end user request in these machines. E.g., let us say
that we want to generate identical Orders in a store machine and a cloud machine. The IDs of the
Order cannot differ between multiple machines.
If you use this IDGenerator, then the IDs will be the same since they all are based out of a request ID that is shared between the replicating machines.
Class needs to be used statically since it is designed to be called by entities in their JPA \@PrePersist and \@PreUpdate methods.
If you use this IDGenerator, then the IDs will be the same since they all are based out of a request ID that is shared between the replicating machines.
Class needs to be used statically since it is designed to be called by entities in their JPA \@PrePersist and \@PreUpdate methods.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
generateID
(String prefix) This method generates a new unique ID for the given prefix.private static int
obtainCounter
(String prefix)
-
Field Details
-
ID_MAP
- See Also:
-
-
Constructor Details
-
IDGenerator
public IDGenerator()
-
-
Method Details
-
generateID
This method generates a new unique ID for the given prefix. The ID is based out of request ID. A counter is added in case multiple IDs are required for the same entity.- Parameters:
prefix
- Prefix that needs to be used. Typically, each entity will have its own prefix.- Returns:
- the ID that can be used to generate new records for the given entity.
-
obtainCounter
-
obtainIdMap
-