use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.
the class TransactionAwareEventForwarderIT method testTwoTransactions.
@Test
public void testTwoTransactions() {
m_transTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
Event event = sendEvent();
getEventAnticipator().anticipateEvent(event);
}
});
m_transTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
Event event = sendEvent();
getEventAnticipator().anticipateEvent(event);
}
});
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.
the class NodeCacheImpl method getNodeAndCategoryInfo.
private Map<String, String> getNodeAndCategoryInfo(Long nodeId) {
final Map<String, String> result = new HashMap<>();
// safety check
if (nodeId != null) {
LOG.debug("Fetching node data from database into cache");
// wrap in a transaction so that Hibernate session is bound and getCategories works
transactionOperations.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
OnmsNode node = nodeDao.get(nodeId.toString());
if (node != null) {
populateBodyWithNodeInfo(result, node);
}
}
});
}
return result;
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.
the class AlarmLifecycleEventsIT method canGenerateAlarmDeletedLifecycleEvents.
@Test
public void canGenerateAlarmDeletedLifecycleEvents() {
// Expect an alarmCreated event
m_eventMgr.getEventAnticipator().resetAnticipated();
m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
// Send a nodeDown
sendNodeDownEvent(1);
// Wait until we've received the alarmCreated event
await().until(allAnticipatedEventsWereReceived());
// Expect an alarmCreated and a alarmCleared event
m_eventMgr.getEventAnticipator().resetAnticipated();
m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CLEARED_UEI, "alarmd").getEvent());
m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
// Send a nodeUp
sendNodeUpEvent(1);
// Wait until we've received the alarmCreated and alarmCleared events
// We need to wait for the cosmicClear automation, which currently runs every 30 seconds
await().atMost(1, MINUTES).until(allAnticipatedEventsWereReceived());
// Expect an alarmDeleted event
m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_DELETED_EVENT_UEI, "alarmd").getEvent());
m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
// We need to wait for the cleanUp automation, which currently runs every 60 seconds
// but it will only trigger then 'lastautomationtime' and 'lasteventtime' < "5 minutes ago"
// so we cheat a little and update the timestamps ourselves instead of waiting
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
Criteria criteria = new Criteria(OnmsAlarm.class);
criteria.addRestriction(new EqRestriction("node.id", 1));
criteria.addRestriction(new EqRestriction("uei", EventConstants.NODE_DOWN_EVENT_UEI));
for (OnmsAlarm alarm : m_alarmDao.findMatching(criteria)) {
LocalDateTime tenMinutesAgo = LocalDateTime.now().minusMinutes(10);
Date then = Date.from(tenMinutesAgo.toInstant(ZoneOffset.UTC));
alarm.setLastAutomationTime(then);
alarm.setLastEventTime(then);
m_alarmDao.save(alarm);
}
m_alarmDao.flush();
}
});
// Wait until we've received the alarmDeleted event
await().atMost(2, MINUTES).until(allAnticipatedEventsWereReceived());
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.
the class AlarmRestServiceIT method afterServletStart.
@Override
protected void afterServletStart() {
MockLogAppender.setupLogging(true, "DEBUG");
m_template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
m_databasePopulator.populateDatabase();
}
});
}
use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.
the class InsSession method getEventsByCriteria.
private void getEventsByCriteria() {
LOG.debug("clearing events");
clearEvents();
final BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
final EventDao eventDao = BeanUtils.getBean(bf, "eventDao", EventDao.class);
final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
try {
transTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
LOG.debug("Entering transaction call back: selection with criteria: {}", criteriaRestriction);
final OnmsCriteria criteria = new OnmsCriteria(OnmsEvent.class);
criteria.add(Restrictions.sqlRestriction(criteriaRestriction));
final List<OnmsEvent> events = eventDao.findMatching(criteria);
LOG.info("Found {} event(s) with criteria: {}", events.size(), criteriaRestriction);
for (final OnmsEvent onmsEvent : events) {
final Event xmlEvent = getXMLEvent(onmsEvent);
if (xmlEvent != null)
addEvent(xmlEvent);
}
}
});
} catch (final RuntimeException e) {
LOG.error("Error while getting events.", e);
}
}
Aggregations