Search in sources :

Example 1 with EventTopic

use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.

the class InMemoryEventBusTest method testSubscribe.

@Test
public void testSubscribe() throws Exception {
    EventTopic topic = mock(EventTopic.class);
    EventSubscriber subscriber = mock(EventSubscriber.class);
    InMemoryEventBus bus = new InMemoryEventBus();
    UUID uuid = bus.subscribe(topic, subscriber);
    assertNotNull(uuid);
    String uuidStr = uuid.toString();
    assertTrue(UuidUtils.validateUUID(uuidStr));
    assertTrue(bus.totalSubscribers() == 1);
    bus.unsubscribe(uuid, subscriber);
    assertTrue(bus.totalSubscribers() == 0);
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) EventTopic(org.apache.cloudstack.framework.events.EventTopic) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with EventTopic

use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.

the class InMemoryEventBusTest method testSubscribeFailSubscriber.

@Test(expected = EventBusException.class)
public void testSubscribeFailSubscriber() throws Exception {
    EventTopic topic = mock(EventTopic.class);
    InMemoryEventBus bus = new InMemoryEventBus();
    bus.subscribe(topic, null);
}
Also used : EventTopic(org.apache.cloudstack.framework.events.EventTopic) Test(org.junit.Test)

Example 3 with EventTopic

use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.

the class InMemoryEventBus method subscribe.

@Override
public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws EventBusException {
    if (subscriber == null || topic == null) {
        throw new EventBusException("Invalid EventSubscriber/EventTopic object passed.");
    }
    UUID subscriberId = UUID.randomUUID();
    subscribers.put(subscriberId, new Pair<EventTopic, EventSubscriber>(topic, subscriber));
    return subscriberId;
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) EventTopic(org.apache.cloudstack.framework.events.EventTopic) EventBusException(org.apache.cloudstack.framework.events.EventBusException) UUID(java.util.UUID)

Example 4 with EventTopic

use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.

the class InMemoryEventBus method publish.

@Override
public void publish(Event event) throws EventBusException {
    if (subscribers == null || subscribers.isEmpty()) {
        // no subscriber to publish to, so just return
        return;
    }
    for (UUID subscriberId : subscribers.keySet()) {
        Pair<EventTopic, EventSubscriber> subscriberDetails = subscribers.get(subscriberId);
        // if the event matches subscribers interested event topic then call back the subscriber with the event
        if (isEventMatchesTopic(event, subscriberDetails.first())) {
            EventSubscriber subscriber = subscriberDetails.second();
            subscriber.onEvent(event);
        }
    }
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) EventTopic(org.apache.cloudstack.framework.events.EventTopic) UUID(java.util.UUID)

Example 5 with EventTopic

use of org.apache.cloudstack.framework.events.EventTopic in project cloudstack by apache.

the class InMemoryEventBusTest method testUnsubscribe.

@Test
public void testUnsubscribe() throws Exception {
    EventTopic topic = mock(EventTopic.class);
    EventSubscriber subscriber = mock(EventSubscriber.class);
    InMemoryEventBus bus = new InMemoryEventBus();
    UUID uuid = bus.subscribe(topic, subscriber);
    assertNotNull(uuid);
    String uuidStr = uuid.toString();
    assertTrue(UuidUtils.validateUUID(uuidStr));
    assertTrue(bus.totalSubscribers() == 1);
    //
    bus.unsubscribe(uuid, subscriber);
    assertTrue(bus.totalSubscribers() == 0);
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) EventTopic(org.apache.cloudstack.framework.events.EventTopic) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

EventTopic (org.apache.cloudstack.framework.events.EventTopic)6 UUID (java.util.UUID)5 EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)5 Test (org.junit.Test)4 Event (org.apache.cloudstack.framework.events.Event)1 EventBusException (org.apache.cloudstack.framework.events.EventBusException)1