Search in sources :

Example 16 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class PersistedSequencingMixin method newSequenceValue.

@Override
public Long newSequenceValue() throws SequencingException {
    synchronized (this) {
        ConcurrentEntityModificationException exc = null;
        UnitOfWork uow = uowf.newUnitOfWork();
        try {
            for (int i = 0; i < 3; i++) {
                try {
                    Property<Long> property = sequence.get().currentValue();
                    long value = property.get();
                    value = value + 1;
                    property.set(value);
                    uow.complete();
                    return value;
                } catch (ConcurrentEntityModificationException e) {
                    // Ignore;
                    exc = e;
                }
            }
            throw new SequencingException("Unable to update sequence value.", exc);
        } catch (UnitOfWorkCompletionException e) {
            throw new SequencingException("Unable to update sequence value.", exc);
        }
    }
}
Also used : ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Example 17 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class HasUoWFileTest method testRetry.

@Test
public void testRetry() throws IOException, UnitOfWorkCompletionException, InterruptedException {
    LOGGER.info("# Test Retry #################################################################################");
    final String entityId;
    File attachedFile;
    // Create new
    try (UnitOfWork uow = module.newUnitOfWork()) {
        TestedEntity entity = createTestedEntity(uow, "Testing Concurrent Modification");
        entityId = entity.identity().get();
        attachedFile = entity.attachedFile();
        uow.complete();
    }
    final List<Exception> ex = new ArrayList<>();
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                testService.modifyFileWithRetry(entityId, 0, 3000);
            } catch (Exception ex1) {
                ex.add(ex1);
            }
        }
    }, "job1");
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                testService.modifyFileWithRetry(entityId, 1000, 0);
            } catch (Exception ex1) {
                ex.add(ex1);
            }
        }
    }, "job2");
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    for (Exception eachEx : ex) {
        eachEx.printStackTrace();
    }
    assertTrue("There were errors during TestRetry", ex.isEmpty());
    assertTrue("Modified file content was not the good one", isFileFirstLineEqualsTo(attachedFile, "Modification"));
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ArrayList(java.util.ArrayList) File(java.io.File) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ConcurrentUoWFileModificationException(org.qi4j.library.uowfile.internal.ConcurrentUoWFileModificationException) IOException(java.io.IOException) AssemblyException(org.qi4j.bootstrap.AssemblyException) Test(org.junit.Test)

Example 18 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class HasUoWFilesTest method testRetry.

@Test
public void testRetry() throws IOException, UnitOfWorkCompletionException, InterruptedException {
    LOGGER.info("# Test Retry #################################################################################");
    final String entityId;
    File attachedFile;
    // Create new
    try (UnitOfWork uow = module.newUnitOfWork()) {
        TestedEntity entity = createTestedOneEntityTwoFilesEntity(uow, "Testing Concurrent Modification");
        entityId = entity.identity().get();
        attachedFile = entity.attachedFile(MyEnum.fileOne);
        uow.complete();
    }
    final List<Exception> ex = new ArrayList<>();
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                testService.modifyFileWithRetry(entityId, 0, 10000);
            } catch (Exception ex1) {
                ex.add(ex1);
            }
        }
    }, "job1");
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                testService.modifyFileWithRetry(entityId, 5000, 0);
            } catch (Exception ex1) {
                ex.add(ex1);
            }
        }
    }, "job2");
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    for (Exception eachEx : ex) {
        eachEx.printStackTrace();
    }
    assertTrue("There were errors during TestRetry", ex.isEmpty());
    assertTrue("Modified file content was not the good one", isFileFirstLineEqualsTo(attachedFile, "Modification"));
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ArrayList(java.util.ArrayList) File(java.io.File) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ConcurrentUoWFileModificationException(org.qi4j.library.uowfile.internal.ConcurrentUoWFileModificationException) IOException(java.io.IOException) AssemblyException(org.qi4j.bootstrap.AssemblyException) Test(org.junit.Test)

Example 19 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class WicketQi4jApplication method handleUnitOfWork.

private void handleUnitOfWork() {
    getRequestCycleListeners().add(new AbstractRequestCycleListener() {

        @Override
        public void onBeginRequest(final RequestCycle requestCycle) {
            super.onBeginRequest(requestCycle);
            logger.debug("================================");
            logger.debug("REQUEST start");
            logger.debug(requestCycle.getRequest().toString());
            logger.debug(requestCycle.getRequest().getRequestParameters().toString());
            UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("REQUEST"));
            logger.debug("  ### NEW " + uow + "   ### MODULE: " + qi4jModule);
        }

        @Override
        public void onEndRequest(final RequestCycle requestCycle) {
            UnitOfWork uow = module.currentUnitOfWork();
            if (uow != null) {
                try {
                    if ("POST".equals(((HttpServletRequest) requestCycle.getRequest().getContainerRequest()).getMethod())) {
                        // "Save"
                        logger.debug("  ### COMPLETE " + uow + "   ### MODULE: " + qi4jModule);
                        uow.complete();
                    } else {
                        // GET requests
                        logger.debug("  ### DISCARD " + uow + "   ### MODULE: " + qi4jModule);
                        uow.discard();
                    }
                } catch (UnitOfWorkCompletionException e) {
                    logger.error("  ### DISCARD " + uow + "   ### MODULE: " + qi4jModule);
                    uow.discard();
                    e.printStackTrace();
                }
            }
            logger.debug("REQUEST end");
            logger.debug("------------------------------------");
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) AbstractRequestCycleListener(org.apache.wicket.request.cycle.AbstractRequestCycleListener)

Example 20 with UnitOfWorkCompletionException

use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.

the class EnvisageSample method createTestData.

public void createTestData() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        createCar("Volvo", "S80", 2007);
        createCar("Volvo", "C70", 2006);
        createCar("Ford", "Transit", 2007);
        createCar("Ford", "Mustang", 2007);
        createCar("Ford", "Mustang", 2006);
        createCar("Ford", "Mustang", 2005);
        createAnimal("Cat", "Miaow");
        createAnimal("Duck", "Kwek Kwek");
        createAnimal("Dog", "Guk Guk");
        createAnimal("Cow", "Moooo");
        uow.complete();
    } catch (UnitOfWorkCompletionException e) {
        // Can not happen.
        e.printStackTrace();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Aggregations

UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)20 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)18 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)12 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Test (org.junit.Test)5 Serializable (java.io.Serializable)4 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)3 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)3 AssemblyException (org.qi4j.bootstrap.AssemblyException)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 File (java.io.File)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)2 ConcurrentUoWFileModificationException (org.qi4j.library.uowfile.internal.ConcurrentUoWFileModificationException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2