use of org.apache.cloudstack.framework.events.EventSubscriber 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;
}
use of org.apache.cloudstack.framework.events.EventSubscriber 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);
}
}
}
use of org.apache.cloudstack.framework.events.EventSubscriber 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);
}
use of org.apache.cloudstack.framework.events.EventSubscriber in project cloudstack by apache.
the class RabbitMQEventBus method unsubscribe.
@Override
public void unsubscribe(UUID subscriberId, EventSubscriber subscriber) throws EventBusException {
try {
String classname = subscriber.getClass().getName();
String queueName = UUID.nameUUIDFromBytes(classname.getBytes()).toString();
Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
Channel channel = queueDetails.second();
channel.basicCancel(queueName);
s_subscribers.remove(queueName, queueDetails);
} catch (Exception e) {
throw new EventBusException("Failed to unsubscribe from event bus due to " + e.getMessage());
}
}
use of org.apache.cloudstack.framework.events.EventSubscriber in project cloudstack by apache.
the class InMemoryEventBusTest method testSubscribeFailTopic.
@Test(expected = EventBusException.class)
public void testSubscribeFailTopic() throws Exception {
EventSubscriber subscriber = mock(EventSubscriber.class);
InMemoryEventBus bus = new InMemoryEventBus();
bus.subscribe(null, subscriber);
}
Aggregations