public static DistributedSchedulingLock updateNGetDistributedScheduleByType(EntityManager em,LockType type,int jobsBlockSize,long schedulingRefreshInterval) throws OptimisticLockException {
  EntityTransaction tx=null;
  long id=type.ordinal() + 1;
  try {
    tx=em.getTransaction();
    tx.begin();
    DistributedSchedulingLock distributedSchedulingLock=getDistributedScheduleByType(em,id);
    if (distributedSchedulingLock == null) {
      distributedSchedulingLock=new DistributedSchedulingLock(id);
      distributedSchedulingLock.setCurrentIndex(jobsBlockSize);
      distributedSchedulingLock.setJobCount(getTotalEnabledJobCount(em,type));
      distributedSchedulingLock.setNextScheduleStartTime(_toMinutes(System.currentTimeMillis() + schedulingRefreshInterval));
      distributedSchedulingLock=em.merge(distributedSchedulingLock);
      em.flush();
    }
 else     if (System.currentTimeMillis() > distributedSchedulingLock.getNextScheduleStartTime()) {
      distributedSchedulingLock.setCurrentIndex(jobsBlockSize);
      distributedSchedulingLock.setJobCount(getTotalEnabledJobCount(em,type));
      distributedSchedulingLock.setNextScheduleStartTime(_toMinutes(System.currentTimeMillis() + schedulingRefreshInterval));
      distributedSchedulingLock=em.merge(distributedSchedulingLock);
      em.flush();
    }
 else {
      if ((distributedSchedulingLock.getCurrentIndex() - jobsBlockSize) < distributedSchedulingLock.getJobCount()) {
        distributedSchedulingLock.setCurrentIndex(distributedSchedulingLock.getCurrentIndex() + jobsBlockSize);
        distributedSchedulingLock=em.merge(distributedSchedulingLock);
        em.flush();
      }
    }
    tx.commit();
    return distributedSchedulingLock;
  }
 catch (  OptimisticLockException ex) {
    if (tx != null && tx.isActive()) {
      tx.rollback();
    }
    throw ex;
  }
}

public static DistributedSchedulingLock updateNGetDistributedScheduleByType ( EntityManager em , LockType type , int jobsBlockSize , long schedulingRefreshInterval ) throws OptimisticLockException { EntityTransaction tx = null ; long id = type . ordinal ( ) + 1 ; try { tx = em . getTransaction ( ) ; tx . begin ( ) ; DistributedSchedulingLock distributedSchedulingLock = getDistributedScheduleByType ( em , id ) ; if ( distributedSchedulingLock == null ) { distributedSchedulingLock = new DistributedSchedulingLock ( id ) ; distributedSchedulingLock . setCurrentIndex ( jobsBlockSize ) ; distributedSchedulingLock . setJobCount ( getTotalEnabledJobCount ( em , type ) ) ; distributedSchedulingLock . setNextScheduleStartTime ( _toMinutes ( System . currentTimeMillis ( ) + schedulingRefreshInterval ) ) ; distributedSchedulingLock = em . merge ( distributedSchedulingLock ) ; em . flush ( ) ; } else if ( System . currentTimeMillis ( ) > distributedSchedulingLock . getNextScheduleStartTime ( ) ) { distributedSchedulingLock . setCurrentIndex ( jobsBlockSize ) ; distributedSchedulingLock . setJobCount ( getTotalEnabledJobCount ( em , type ) ) ; distributedSchedulingLock . setNextScheduleStartTime ( _toMinutes ( System . currentTimeMillis ( ) + schedulingRefreshInterval ) ) ; distributedSchedulingLock = em . merge ( distributedSchedulingLock ) ; em . flush ( ) ; } else { if ( ( distributedSchedulingLock . getCurrentIndex ( ) - jobsBlockSize ) < distributedSchedulingLock . getJobCount ( ) ) { distributedSchedulingLock . setCurrentIndex ( distributedSchedulingLock . getCurrentIndex ( ) + jobsBlockSize ) ; distributedSchedulingLock = em . merge ( distributedSchedulingLock ) ; em . flush ( ) ; } } tx . commit ( ) ; return distributedSchedulingLock ; } catch ( OptimisticLockException ex ) { if ( tx != null && tx . isActive ( ) ) { tx . rollback ( ) ; } throw ex ; } }
obtains a distributed schedule object of a given type .
