SequenceGeneratorBean.java~14~
上传用户:jnhyscl
上传日期:2010-03-08
资源大小:345k
文件大小:3k
源码类别:

电子政务应用

开发平台:

Java

  1. package shiponline;
  2. import javax.ejb.*;
  3. public class SequenceGeneratorBean implements SessionBean {
  4.   SessionContext sessionContext;
  5.   private class Entry {
  6.    Sequence sequence;
  7.    int first = 0;
  8.    int last = Integer.MAX_VALUE;
  9.  };
  10.  private java.util.Hashtable entries = new java.util.Hashtable();
  11.  private int blockSize;
  12.  private int retryCount;
  13.  private SequenceHome sequenceHome;
  14.  public void ejbCreate() throws CreateException {
  15.     /**@todo Complete this method*/
  16.   }
  17.   public void ejbRemove() {
  18.     /**@todo Complete this method*/
  19.   }
  20.   public void ejbActivate() {
  21.     /**@todo Complete this method*/
  22.   }
  23.   public void ejbPassivate() {
  24.     /**@todo Complete this method*/
  25.   }
  26.   public void setSessionContext(SessionContext sessionContext) {
  27.     try {
  28.      javax.naming.Context context = new javax.naming.InitialContext();
  29.      blockSize=1;
  30.      retryCount=5;
  31.      //blockSize = ((Integer) context.lookup("java:comp/env/blockSize")).intValue();
  32.      //retryCount =/((Integer) context.lookup("java:comp/env/retryCount")).intValue();
  33.      sequenceHome = (SequenceHome) context.lookup("java:comp/env/Sequence");
  34.    }
  35.    catch(javax.naming.NamingException e) {
  36.      throw new javax.ejb.EJBException(e);
  37.    }
  38.   }
  39.   public int nextSequenceNumber(String name) {
  40.     try {
  41.          Entry entry = (Entry) entries.get(name);
  42.          System.out.println("name="+name);
  43.          if(entry == null) {
  44.            // add an entry to the sequence table
  45.            entry = new Entry();
  46.            try {
  47.              System.out.println("yanglongfei");
  48.              entry.sequence = sequenceHome.findByPrimaryKey(name);
  49.                 System.out.println("name1="+name);
  50.            }
  51.            catch(javax.ejb.FinderException e) {
  52.              // if we couldn't find it, then create it...
  53.              System.out.println("longfeiyang");
  54.              entry.sequence = sequenceHome.create(name);
  55.            }
  56.            entries.put(name, entry);
  57.          }
  58.          if(entry.last > entry.first + blockSize) {
  59.            for(int retry = 0; true; retry++) {
  60.              try {
  61.                entry.first = entry.sequence.seqValueBeforeIncrementingBy(blockSize);
  62.                entry.last = entry.first;
  63.                break;
  64.              }
  65.              catch(Exception e) {
  66.                if(retry < retryCount) {
  67.                  // we hit a concurrency exception, so try again...
  68.                  continue;
  69.                }
  70.                else {
  71.                  // we tried too many times, so fail...
  72.                  throw new javax.ejb.EJBException(e);
  73.                }
  74.              }
  75.            }
  76.          }
  77.          return entry.last++;
  78.        }
  79.        catch(javax.ejb.CreateException e) {
  80.          throw new javax.ejb.EJBException(e);
  81.        }
  82.        catch(Exception e) {
  83.          throw new javax.ejb.EJBException(e);
  84.        }
  85.   }
  86. }