Search in sources :

Example 6 with AggregationCompleted

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

the class AggregationDAOImpl method getAggregationCompleted.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public AggregationCompleted getAggregationCompleted(String id, boolean lock) throws AggregationDAOException {
    validateState();
    try {
        if (!lock) {
            // it's not locked, so just find the completed aggregation and return it
            return entityManager.find(AggregationCompleted.class, id);
        } else {
            /* this is essentially a recovery of a failed completed exchange
				* camel runs this on start up and incremental intervals... it is
				* essential that two application or route instances do not try to play
				* back the same exchange at the same time, or else duplicate exchanges may be
				* routed.... the recovery needs to be locked
				*/
            // first check to see if the recovery is available for locking
            final AggregationCompleted entity = entityManager.find(AggregationCompleted.class, id);
            if (entity == null)
                return null;
            // if the recovery lock time has not passed, then we can't recover
            if (entity.getRecoveryLockedUntilDtTm() != null && entity.getRecoveryLockedUntilDtTm().after(Calendar.getInstance(Locale.getDefault())))
                return null;
            // now lock the row and update lock time and the count
            entityManager.lock(entity, LockModeType.WRITE);
            final Calendar newRecoveryLockTime = Calendar.getInstance(Locale.getDefault());
            newRecoveryLockTime.add(Calendar.SECOND, recoveredEntityLockInterval);
            entity.setRecoveryLockedUntilDtTm(newRecoveryLockTime);
            // persist the time new lock time and increment the update count
            entityManager.persist(entity);
            entityManager.flush();
            return entity;
        }
    } catch (OptimisticLockException ol) {
        // someone else locked the row... return null
        return null;
    } catch (Exception e) {
        throw new AggregationDAOException(e);
    }
}
Also used : Calendar(java.util.Calendar) 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 AggregationCompleted

use of org.nhindirect.monitor.dao.entity.AggregationCompleted 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 8 with AggregationCompleted

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

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Example 9 with AggregationCompleted

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

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
    final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
    assertNull(lockedAggr);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Example 10 with AggregationCompleted

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

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
    Thread.sleep(4000);
    final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(lockedAggr);
    assertEquals("12345", lockedAggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), lockedAggr.getExchangeBlob()));
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Aggregations

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