use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.
the class EventIpcManagerDefaultImplTest method testBroadcastNowSync.
public void testBroadcastNowSync() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
final EventListener slowListener = new EventListener() {
@Override
public String getName() {
return "testBroadcastNowSync";
}
@Override
public void onEvent(Event event) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
} catch (InterruptedException e) {
}
counter.incrementAndGet();
}
};
EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
manager.setHandlerPoolSize(5);
DefaultEventHandlerImpl handler = new DefaultEventHandlerImpl(m_registry);
manager.setEventHandler(handler);
manager.afterPropertiesSet();
manager.addEventListener(slowListener);
EventBuilder bldr = new EventBuilder("uei.opennms.org/foo", "testBroadcastNowSync");
Event e = bldr.getEvent();
// Verify the initial state
assertEquals(0, counter.get());
// Broadcast synchronously - this should block until our event listener returns
manager.broadcastNow(e, true);
// broadcastNow() returned, so the counter should have been increased
assertEquals(1, counter.get());
}
use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.
the class EventIpcManagerDefaultImplTest method testRecursiveEvents.
/**
* This test creates two event listeners that both create events as they
* handle events. This test can be used to detect deadlocks between the
* listeners.
*
* @throws InterruptedException
*/
public void testRecursiveEvents() throws InterruptedException {
final int numberOfEvents = 20;
CountDownLatch fooCounter = new CountDownLatch(numberOfEvents);
CountDownLatch barCounter = new CountDownLatch(numberOfEvents);
CountDownLatch kiwiCounter = new CountDownLatch(numberOfEvents);
CountDownLatch ulfCounter = new CountDownLatch(numberOfEvents);
final EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
manager.setHandlerPoolSize(1);
// manager.setHandlerQueueLength(5);
DefaultEventHandlerImpl handler = new DefaultEventHandlerImpl(m_registry);
manager.setEventHandler(handler);
manager.afterPropertiesSet();
EventListener slowFooBarListener = new EventListener() {
@Override
public String getName() {
return "slowFooBarListener";
}
@Override
public void onEvent(Event event) {
if ("uei.opennms.org/foo".equals(event.getUei())) {
EventBuilder bldr = new EventBuilder("uei.opennms.org/bar", "testRecursiveEvents");
Event e = bldr.getEvent();
manager.broadcastNow(e, false);
fooCounter.countDown();
} else {
try {
Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
} catch (InterruptedException e) {
}
barCounter.countDown();
}
}
};
EventListener slowKiwiUlfListener = new EventListener() {
@Override
public String getName() {
return "slowKiwiUlfListener";
}
@Override
public void onEvent(Event event) {
if ("uei.opennms.org/foo".equals(event.getUei())) {
EventBuilder bldr = new EventBuilder("uei.opennms.org/ulf", "testRecursiveEvents");
Event e = bldr.getEvent();
manager.broadcastNow(e, false);
kiwiCounter.countDown();
} else {
try {
Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
} catch (InterruptedException e) {
}
ulfCounter.countDown();
}
}
};
manager.addEventListener(slowFooBarListener);
manager.addEventListener(slowKiwiUlfListener);
//
for (int i = 0; i < numberOfEvents; i++) {
EventBuilder bldr = new EventBuilder("uei.opennms.org/foo", "testRecursiveEvents");
Event e = bldr.getEvent();
manager.broadcastNow(e, false);
}
assertTrue("foo counter not satisfied: " + fooCounter.getCount(), fooCounter.await(100, TimeUnit.SECONDS));
assertTrue("bar counter not satisfied: " + barCounter.getCount(), barCounter.await(100, TimeUnit.SECONDS));
assertTrue("kiwi counter not satisfied: " + kiwiCounter.getCount(), kiwiCounter.await(100, TimeUnit.SECONDS));
assertTrue("ulf counter not satisfied: " + ulfCounter.getCount(), ulfCounter.await(100, TimeUnit.SECONDS));
}
use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.
the class AMQPEventReceiverBlueprintTest method canReceiveEvent.
@Test
public void canReceiveEvent() throws Exception {
// Register an event listener
final List<Event> receivedEvents = Lists.newArrayList();
eventIpcManager.addEventListener(new EventListener() {
@Override
public String getName() {
return "test";
}
@Override
public void onEvent(Event e) {
receivedEvents.add(e);
}
});
// Send a event, simulating an event on the source "queue"
Event event = new Event();
template.sendBody("direct:source", event);
// Wait until the event is received by our listener
assertMockEndpointsSatisfied();
for (int i = 0; i < 60; i += 5) {
if (receivedEvents.size() != 0) {
break;
}
Thread.sleep(5);
}
assertEquals(1, receivedEvents.size());
}
use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.
the class EifAdapterBlueprintTest method testCanParseEifPacketsAndGenerateEvents.
@Test
public void testCanParseEifPacketsAndGenerateEvents() throws Exception {
// Register an event listener
final List<Event> receivedEvents = Lists.newArrayList();
eventIpcManager.addEventListener(new EventListener() {
@Override
public String getName() {
return "test";
}
@Override
public void onEvent(Event e) {
receivedEvents.add(e);
}
});
FileInputStream eifPacketCapture = new FileInputStream(new File("src/test/resources/eif_packets_simple_test.dat"));
Socket clientSocket = new Socket(InetAddrUtils.getLocalHostAddress(), 1828);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.write(IOUtils.toByteArray(eifPacketCapture));
outToServer.flush();
clientSocket.close();
await().atMost(15, SECONDS).until(() -> receivedEvents.size() == 6);
for (Event event : receivedEvents) {
assertTrue("UEI must match regex.", event.getUei().matches("^uei.opennms.org/vendor/IBM/EIF/EIF_TEST_EVENT_TYPE_\\w$"));
assertTrue("situation_name must match regex.", event.getParm("situation_name").getValue().getContent().matches("^Situation \\d{2}"));
}
}
use of org.opennms.netmgt.events.api.EventListener in project opennms by OpenNMS.
the class EventIpcManagerDefaultImplTest method testSlowEventListener.
public void testSlowEventListener() throws InterruptedException {
AtomicInteger counter = new AtomicInteger();
EventListener slowListener = new EventListener() {
@Override
public String getName() {
return "testSlowEventListener";
}
@Override
public void onEvent(Event event) {
LOG.info("Hello, here is event: " + event.getUei());
try {
Thread.sleep(SLOW_EVENT_OPERATION_DELAY);
} catch (InterruptedException e) {
}
counter.incrementAndGet();
}
};
EventIpcManagerDefaultImpl manager = new EventIpcManagerDefaultImpl(m_registry);
manager.setHandlerPoolSize(1);
manager.setHandlerQueueLength(5);
DefaultEventHandlerImpl handler = new DefaultEventHandlerImpl(m_registry);
manager.setEventHandler(handler);
manager.afterPropertiesSet();
manager.addEventListener(slowListener);
//
for (int i = 0; i < 10; i++) {
EventBuilder bldr = new EventBuilder("uei.opennms.org/foo/" + i, "testSlowEventListener");
Event e = bldr.getEvent();
manager.broadcastNow(e, false);
}
await().pollInterval(1, TimeUnit.SECONDS).untilAtomic(counter, is(equalTo(6)));
}
Aggregations