Search in sources :

Example 6 with EventListener

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());
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) ThreadAwareEventListener(org.opennms.netmgt.events.api.ThreadAwareEventListener)

Example 7 with EventListener

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));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) ThreadAwareEventListener(org.opennms.netmgt.events.api.ThreadAwareEventListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with EventListener

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());
}
Also used : Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Example 9 with EventListener

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}"));
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) File(java.io.File) FileInputStream(java.io.FileInputStream) Socket(java.net.Socket) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Example 10 with EventListener

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)));
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.opennms.netmgt.xml.event.Event) EventListener(org.opennms.netmgt.events.api.EventListener) ThreadAwareEventListener(org.opennms.netmgt.events.api.ThreadAwareEventListener)

Aggregations

EventListener (org.opennms.netmgt.events.api.EventListener)10 Event (org.opennms.netmgt.xml.event.Event)9 ThreadAwareEventListener (org.opennms.netmgt.events.api.ThreadAwareEventListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 CamelBlueprintTest (org.opennms.core.test.camel.CamelBlueprintTest)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Socket (java.net.Socket)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CompletableFuture (java.util.concurrent.CompletableFuture)1