use of org.apache.ofbiz.entity.util.SequenceUtil in project ofbiz-framework by apache.
the class EntityTestSuite method testSequenceValueItemWithConcurrentThreads.
public void testSequenceValueItemWithConcurrentThreads() {
final SequenceUtil sequencer = new SequenceUtil(delegator.getGroupHelperInfo(delegator.getEntityGroupName("SequenceValueItem")), delegator.getModelEntity("SequenceValueItem"), "seqName", "seqId");
UUID id = UUID.randomUUID();
final String sequenceName = "BogusSequence" + id.toString();
final ConcurrentMap<Long, Long> seqIds = new ConcurrentHashMap<>();
final AtomicBoolean duplicateFound = new AtomicBoolean(false);
final AtomicBoolean nullSeqIdReturned = new AtomicBoolean(false);
List<Future<Void>> futures = new ArrayList<>();
Callable<Void> getSeqIdTask = new Callable<Void>() {
public Void call() throws Exception {
Long seqId = sequencer.getNextSeqId(sequenceName, 1, null);
if (seqId == null) {
nullSeqIdReturned.set(true);
return null;
}
Long existingValue = seqIds.putIfAbsent(seqId, seqId);
if (existingValue != null) {
duplicateFound.set(true);
}
return null;
}
};
Callable<Void> refreshTask = new Callable<Void>() {
public Void call() throws Exception {
sequencer.forceBankRefresh(sequenceName, 1);
return null;
}
};
double probabilityOfRefresh = 0.1;
for (int i = 1; i <= 1000; i++) {
Callable<Void> randomTask = Math.random() < probabilityOfRefresh ? refreshTask : getSeqIdTask;
futures.add(ExecutionPool.GLOBAL_FORK_JOIN.submit(randomTask));
}
long startTime = System.currentTimeMillis();
ExecutionPool.getAllFutures(futures);
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
Debug.logInfo("testSequenceValueItemWithConcurrentThreads total time (ms): " + totalTime, module);
assertFalse("Null sequence id returned", nullSeqIdReturned.get());
assertFalse("Duplicate sequence id returned", duplicateFound.get());
}
use of org.apache.ofbiz.entity.util.SequenceUtil in project ofbiz-framework by apache.
the class EntityTestSuite method testSequenceValueItem.
public void testSequenceValueItem() {
SequenceUtil sequencer = new SequenceUtil(delegator.getGroupHelperInfo(delegator.getEntityGroupName("SequenceValueItem")), delegator.getModelEntity("SequenceValueItem"), "seqName", "seqId");
UUID id = UUID.randomUUID();
String sequenceName = "BogusSequence" + id.toString();
for (int i = 10000; i <= 10015; i++) {
Long seqId = sequencer.getNextSeqId(sequenceName, 1, null);
assertEquals(i, seqId.longValue());
}
sequencer.forceBankRefresh(sequenceName, 1);
Long seqId = sequencer.getNextSeqId(sequenceName, 1, null);
assertEquals(10020, seqId.longValue());
}
use of org.apache.ofbiz.entity.util.SequenceUtil in project ofbiz-framework by apache.
the class GenericDelegator method getNextSeqIdLong.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#getNextSeqIdLong(java.lang.String, long)
*/
public Long getNextSeqIdLong(String seqName, long staggerMax) {
try {
SequenceUtil sequencer = this.AtomicRefSequencer.get();
if (sequencer == null) {
ModelEntity seqEntity = this.getModelEntity("SequenceValueItem");
sequencer = new SequenceUtil(this.getEntityHelperInfo("SequenceValueItem"), seqEntity, "seqName", "seqId");
if (!AtomicRefSequencer.compareAndSet(null, sequencer)) {
sequencer = this.AtomicRefSequencer.get();
}
}
ModelEntity seqModelEntity = null;
try {
seqModelEntity = getModelReader().getModelEntity(seqName);
} catch (GenericEntityException e) {
Debug.logInfo("Entity definition not found for sequence name " + seqName, module);
}
Long newSeqId = sequencer == null ? null : sequencer.getNextSeqId(seqName, staggerMax, seqModelEntity);
return newSeqId;
} catch (Exception e) {
String errMsg = "Failure in getNextSeqIdLong operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
throw new GeneralRuntimeException(errMsg, e);
}
}
Aggregations