Search in sources :

Example 6 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl method removeAggregation.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public void removeAggregation(Aggregation agg, String exchangeId) throws AggregationDAOException {
    try {
        // find the aggregation
        final Aggregation existingAgg = this.getAggregation(agg.getId());
        if (existingAgg != null) {
            // check the version number for consistency
            if (existingAgg.getVersion() != agg.getVersion())
                throw new AggregationVersionException("Version number of aggreation does not match what is in the store.");
            // lock for removal
            entityManager.lock(existingAgg, LockModeType.WRITE);
            entityManager.remove(existingAgg);
        } else
            throw new AggregationDAOException("Aggregation does not exist is store.");
        // add to the completed repository
        final AggregationCompleted completed = new AggregationCompleted();
        completed.setExchangeBlob(existingAgg.getExchangeBlob());
        completed.setId(exchangeId);
        completed.setVersion(1);
        entityManager.persist(completed);
        // commit
        entityManager.flush();
    } catch (AggregationDAOException ae) {
        throw ae;
    }///CLOVER:OFF
     catch (OptimisticLockException ol) {
        throw new AggregationVersionException("Aggregation was removed by another thread or process before it could be committed.");
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to remove aggregation.", e);
    }
///CLOVER:ON
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_entityManagerException_assertNoAggregation.

@Test
public void testAddUpdateAggregationTest_entityManagerException_assertNoAggregation() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new RuntimeException()).when(mgr).persist(any());
    final AggregationDAOImpl dao = new AggregationDAOImpl();
    dao.setEntityManager(mgr);
    boolean exceptionOccured = false;
    try {
        final Aggregation insert = new Aggregation();
        insert.setExchangeBlob(new byte[] { 0, 3, 2 });
        insert.setId("12345");
        insert.setVersion(0);
        dao.addUpdateAggregation(insert);
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) EntityManager(javax.persistence.EntityManager) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Example 8 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class ConcurrentJPAAggregationRepository method add.

/**
	 * {@inheritDoc}
	 * This specific implementation throws a Runtime exception on DAO errors.  This implementation also checks for
	 * consistency/concurrency of the exchange.  If the attempted exchange does match the latest and greatest
	 * exchange version, then an AggregationVersionException is wrapped by the runtime error.  Routes using this
	 * repository should catch the AggregationVersionException and attempt to retry the exchange.  If exception handling
	 * and redelivery is configured correctly, Camel should automatically reload the exchange from the latest version in the 
	 * and attempt the aggregation process again.
	 * 
	 */
@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
    try {
        // serialize the exchange to a blob
        final byte[] blob = codec.marshallExchange(camelContext, exchange).getData();
        // get the current version of the exchange... if this is the first time the exchange with the
        // given key is added, this should result in null
        Integer currentEntityVersion = (Integer) exchange.getProperty(AGGREGATION_ENTITY_VERSON);
        Aggregation agg = new Aggregation();
        agg.setExchangeBlob(blob);
        agg.setId(key);
        agg.setVersion(currentEntityVersion == null ? 0 : currentEntityVersion);
        // add/update the repository... 
        dao.addUpdateAggregation(agg);
        // update the version on the exchange
        exchange.setProperty(AGGREGATION_ENTITY_VERSON, agg.getVersion());
    } catch (Exception e) {
        // wrap exception in a runtime exception
        throw new RuntimeException("Error adding to repository aggregation with key " + key, e);
    }
    // might support getting the older version in some later version
    return null;
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation)

Example 9 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class ConcurrentJPAAggregationRepository method remove.

/**
	 * {@inheritDoc}
	 * 	This specific implementation throws a Runtime exception on DAO errors.  This implementation also checks for
	 * consistency/concurrency of the exchange.  If the attempted removal of an exchange does match the latest and greatest
	 * exchange version, then an AggregationVersionException is wrapped by the runtime error.  Routes using this
	 * repository should catch the AggregationVersionException and attempt to retry the exchange.  If exception handling
	 * and redelivery is configured correctly, Camel should automatically reload the exchange from the latest version in the 
	 * and attempt the aggregation and completion condition again.
	 */
@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
    try {
        // get the version of the exchange
        Integer currentEntityVersion = (Integer) exchange.getProperty(AGGREGATION_ENTITY_VERSON);
        // serialize the exchange to a byte array
        final byte[] blob = codec.marshallExchange(camelContext, exchange).getData();
        Aggregation agg = new Aggregation();
        agg.setExchangeBlob(blob);
        agg.setId(key);
        agg.setVersion(currentEntityVersion == null ? 0 : currentEntityVersion);
        // removed the exchange from the currently working set and move it to completed set
        // for later confirmation
        dao.removeAggregation(agg, exchange.getExchangeId());
    } catch (Exception e) {
        // wrap exception in a runtime exception
        throw new RuntimeException("Error removing from repository aggregation with key " + key, e);
    }
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation)

Example 10 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback.

@Test
public void testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    final Aggregation insert2 = new Aggregation();
    insert2.setExchangeBlob(new byte[] { 0, 3, 2, 4, 1, 32 });
    insert2.setVersion(3);
    insert2.setId("12345");
    boolean exceptionOccured = false;
    try {
        notifDao.addUpdateAggregation(insert2);
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
    final Aggregation aggr = notifDao.getAggregation("12345");
    assertNotNull(aggr);
    assertEquals(insert, aggr);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) Test(org.junit.Test)

Aggregations

Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)25 Test (org.junit.Test)20 AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)7 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)6 OptimisticLockException (javax.persistence.OptimisticLockException)3 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)3 EntityManager (javax.persistence.EntityManager)2 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Exchange (org.apache.camel.Exchange)1 Buffer (org.fusesource.hawtbuf.Buffer)1