public class Mutilkeygenerator {
private static HashMap keys=new HashMap ();
private static final int pool_size=20;
Private KeyInfo Keyinfo=null;
Private Mutilkeygenerator () {}
Private Mutilkeygenerator (String keyName) {
Keyinfo=new KeyInfo (pool_size, keyName);
}
/*
* Static factory to provide its own instance
* */
Public synchronized static Mutilkeygenerator getinstance (String keyName) {
Mutilkeygenerator Keygenerator=null;
if (Keys.containskey (KeyName)) {
Keygenerator= (Mutilkeygenerator) keys.get (keyName);
System.out.println (keyname+ "founded");
}
else{
Keygenerator=new Mutilkeygenerator (keyName);
Keys.put (KeyName, keygenerator);
System.out.println (keyname+ "created");
}
return keygenerator;
}
public int Getnextkey () {
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 multi-example mode of Sequence key generator