Search in sources :

Example 1 with EventSubscriber

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

the class InMemoryEventBusTest method testPublishEmpty.

@Test
public void testPublishEmpty() throws Exception {
    EventSubscriber subscriber = mock(EventSubscriber.class);
    Event event = mock(Event.class);
    InMemoryEventBus bus = new InMemoryEventBus();
    bus.publish(event);
    verify(subscriber, times(0)).onEvent(event);
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) Event(org.apache.cloudstack.framework.events.Event) Test(org.junit.Test)

Example 2 with EventSubscriber

use of org.apache.cloudstack.framework.events.EventSubscriber 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 3 with EventSubscriber

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

the class InMemoryEventBusTest method testUnsubscribeFailWrongUUID.

@Test(expected = EventBusException.class)
public void testUnsubscribeFailWrongUUID() throws Exception {
    EventSubscriber subscriber = mock(EventSubscriber.class);
    InMemoryEventBus bus = new InMemoryEventBus();
    UUID uuid = UUID.randomUUID();
    bus.unsubscribe(uuid, subscriber);
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with EventSubscriber

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

the class RabbitMQEventBus method stop.

@Override
public synchronized boolean stop() {
    if (s_connection.isOpen()) {
        for (String subscriberId : s_subscribers.keySet()) {
            Ternary<String, Channel, EventSubscriber> subscriberDetails = s_subscribers.get(subscriberId);
            Channel channel = subscriberDetails.second();
            String queueName = subscriberId;
            try {
                channel.queueDelete(queueName);
                channel.abort();
            } catch (IOException ioe) {
                s_logger.warn("Failed to delete queue: " + queueName + " on AMQP server due to " + ioe.getMessage());
            }
        }
    }
    closeConnection();
    return true;
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) Channel(com.rabbitmq.client.Channel) IOException(java.io.IOException)

Example 5 with EventSubscriber

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

the class RabbitMQEventBus method subscribe.

/** Call to subscribe to interested set of events
     *
     * @param topic defines category and type of the events being subscribed to
     * @param subscriber subscriber that intends to receive event notification
     * @return UUID that represents the subscription with event bus
     * @throws EventBusException
     */
@Override
public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws EventBusException {
    if (subscriber == null || topic == null) {
        throw new EventBusException("Invalid EventSubscriber/EventTopic object passed.");
    }
    // create a UUID, that will be used for managing subscriptions and also used as queue name
    // for on the queue used for the subscriber on the AMQP broker
    UUID queueId = UUID.randomUUID();
    String queueName = queueId.toString();
    try {
        String bindingKey = createBindingKey(topic);
        // store the subscriber details before creating channel
        s_subscribers.put(queueName, new Ternary(bindingKey, null, subscriber));
        // create a channel dedicated for this subscription
        Connection connection = getConnection();
        Channel channel = createChannel(connection);
        // create a queue and bind it to the exchange with binding key formed from event topic
        createExchange(channel, amqpExchangeName);
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, amqpExchangeName, bindingKey);
        // register a callback handler to receive the events that a subscriber subscribed to
        channel.basicConsume(queueName, s_autoAck, queueName, new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String queueName, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
                if (queueDetails != null) {
                    EventSubscriber subscriber = queueDetails.third();
                    String routingKey = envelope.getRoutingKey();
                    String eventSource = getEventSourceFromRoutingKey(routingKey);
                    String eventCategory = getEventCategoryFromRoutingKey(routingKey);
                    String eventType = getEventTypeFromRoutingKey(routingKey);
                    String resourceType = getResourceTypeFromRoutingKey(routingKey);
                    String resourceUUID = getResourceUUIDFromRoutingKey(routingKey);
                    Event event = new Event(eventSource, eventCategory, eventType, resourceType, resourceUUID);
                    event.setDescription(new String(body));
                    // deliver the event to call back object provided by subscriber
                    subscriber.onEvent(event);
                }
            }
        });
        // update the channel details for the subscription
        Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
        queueDetails.second(channel);
        s_subscribers.put(queueName, queueDetails);
    } catch (AlreadyClosedException closedException) {
        s_logger.warn("Connection to AMQP service is lost. Subscription:" + queueName + " will be active after reconnection");
    } catch (ConnectException connectException) {
        s_logger.warn("Connection to AMQP service is lost. Subscription:" + queueName + " will be active after reconnection");
    } catch (Exception e) {
        throw new EventBusException("Failed to subscribe to event due to " + e.getMessage());
    }
    return queueId;
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Ternary(com.cloud.utils.Ternary) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) Envelope(com.rabbitmq.client.Envelope) ConfigurationException(javax.naming.ConfigurationException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConnectException(java.net.ConnectException) EventBusException(org.apache.cloudstack.framework.events.EventBusException) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) AMQP(com.rabbitmq.client.AMQP) Event(org.apache.cloudstack.framework.events.Event) EventBusException(org.apache.cloudstack.framework.events.EventBusException) UUID(java.util.UUID) ConnectException(java.net.ConnectException)

Aggregations

EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)11 UUID (java.util.UUID)7 Test (org.junit.Test)6 EventTopic (org.apache.cloudstack.framework.events.EventTopic)5 Channel (com.rabbitmq.client.Channel)3 IOException (java.io.IOException)3 Event (org.apache.cloudstack.framework.events.Event)3 EventBusException (org.apache.cloudstack.framework.events.EventBusException)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)2 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)2 ConnectException (java.net.ConnectException)2 ConfigurationException (javax.naming.ConfigurationException)2 Ternary (com.cloud.utils.Ternary)1 AMQP (com.rabbitmq.client.AMQP)1 Connection (com.rabbitmq.client.Connection)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1