Search in sources :

Example 1 with SequenceUtil

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());
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SequenceUtil(org.apache.ofbiz.entity.util.SequenceUtil) Future(java.util.concurrent.Future) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with SequenceUtil

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());
}
Also used : SequenceUtil(org.apache.ofbiz.entity.util.SequenceUtil) UUID(java.util.UUID)

Example 3 with SequenceUtil

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);
    }
}
Also used : SequenceUtil(org.apache.ofbiz.entity.util.SequenceUtil) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SAXException(org.xml.sax.SAXException) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

SequenceUtil (org.apache.ofbiz.entity.util.SequenceUtil)3 UUID (java.util.UUID)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)1 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)1 SerializeException (org.apache.ofbiz.entity.serialize.SerializeException)1 SAXException (org.xml.sax.SAXException)1