public class Keygenerator {
private static Keygenerator keygen=new keygenerator ();
private static final int pool_size=20;
Private HashMap keylist=new HashMap (10);
Private Keygenerator () {}
public static Keygenerator Geinstance () {
return keygen;
}
/* Value method, provide the next appropriate key value */
public synchronized int Getnextkey (String keyName)
{
KeyInfo Keyinfo=null;
if (Keylist.containskey (KeyName)) {
keyinfo= (KeyInfo) keylist.get (keyName);
System.out.println ("key found");
}
else{
Keyinfo=new KeyInfo (pool_size, keyName);
Keylist.put (KeyName, keyInfo);
SYSTEM.OUT.PRINTLN ("New key created");
}
return Keyinfo.getnextkey ();
}
}
Class KeyInfo
{
private int Keymax;
private int keymin;
private int nextkey;
private int poolsize;
Private String KeyName;
Public KeyInfo (int poolsize,string keyName)
{
This.poolsize=poolsize;
This.keyname=keyname;
Retrivefromdb ();
}
public int Getkeymax () {
return Keymax;
}
public int getkeymin () {
return keymin;
}
public int Getnextkey ()
{
if (Nextkey>keymax) {
Retrivefromdb ();
}
return nextkey++;
}
/*
* Extract current key values from the database
* */
private void Retrivefromdb ()
{
String sql1= "Update keyTable set keyvalue=keyvalue+" +poolsize+ "where Keyname= '" +keyname+ "'";
String sql2= "Select KeyValue from keyTable where Keyname= '" +keyname+ "'";
Excute the Sql,assume the value returned is 1000
int keyfromdb=1000;
This.keymax=keyfromdb;
this.keymin=keyfromdb-poolsize+1;
This.nextkey=keymin;
}
}
Implementation of single-instance mode of Sequence key generator