Listing 1
/**
* Singleton exists on a per JVM basis and
returns a new, unique ID
* to the caller.
*/
public class Singleton {
private long max = 0;
private long currentID
= 0;
private static Singleton
singleton = new Singleton();
private UniqueID uniqueIDEJB;
/**
* Accessor to get handle
to singleton object.
*/
public Singleton instance()
{
return singleton;
}
/**
* Cannot instantiate.
Must use instance() method to get a handle to the object.
*/
private Singleton() {
// GET HANDLE TO UNIQUEID EJB
}
/**
* Returns a unique ID
to the caller. This is synchronized to prevent
* multiple callers from
getting the same ID back if they called getNextID() at the
* same time.
*
*
* @return long
next ID
*/
public synchronized long
getNextID() throws Exception{
currentID++;
if (currentID > max) {
try{
IDSet set = uniqueIdEJB.getNextIDSet();
// reset current id
currentID = set.getMin();
max = set.getMax();
}
catch(Exception
ex) {
ex.printStackTrace();
throw ex;
}
}
return currentID;
}