Search in sources :

Example 1 with EventSubscriber

use of com.cloud.framework.events.EventSubscriber in project cosmic by MissionCriticalCloud.

the class RabbitMQEventBus method unsubscribe.

@Override
public void unsubscribe(final UUID subscriberId, final EventSubscriber subscriber) throws EventBusException {
    try {
        final String classname = subscriber.getClass().getName();
        final String queueName = UUID.nameUUIDFromBytes(classname.getBytes()).toString();
        final Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
        final Channel channel = queueDetails.second();
        channel.basicCancel(queueName);
        s_subscribers.remove(queueName, queueDetails);
    } catch (final IOException e) {
        throw new EventBusException("Failed to unsubscribe from event bus due to " + e.getMessage(), e);
    }
}
Also used : EventSubscriber(com.cloud.framework.events.EventSubscriber) Channel(com.rabbitmq.client.Channel) EventBusException(com.cloud.framework.events.EventBusException) IOException(java.io.IOException)

Example 2 with EventSubscriber

use of com.cloud.framework.events.EventSubscriber in project cosmic by MissionCriticalCloud.

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(final EventTopic topic, final 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
    final UUID queueId = UUID.randomUUID();
    final String queueName = queueId.toString();
    try {
        final 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
        final Connection connection = getConnection();
        final 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(final String queueName, final Envelope envelope, final AMQP.BasicProperties properties, final byte[] body) throws IOException {
                RabbitMQEventBus.this.handleDelivery(queueName, envelope, body);
            }
        });
        // update the channel details for the subscription
        final Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
        queueDetails.second(channel);
        s_subscribers.put(queueName, queueDetails);
    } catch (final AlreadyClosedException | ConnectException | NoSuchAlgorithmException | KeyManagementException | TimeoutException e) {
        s_logger.warn("Connection to AMQP service is lost. Subscription:" + queueName + " will be active after reconnection", e);
    } catch (final IOException e) {
        throw new EventBusException("Failed to subscribe to event due to " + e.getMessage(), e);
    }
    return queueId;
}
Also used : EventSubscriber(com.cloud.framework.events.EventSubscriber) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Envelope(com.rabbitmq.client.Envelope) KeyManagementException(java.security.KeyManagementException) AMQP(com.rabbitmq.client.AMQP) EventBusException(com.cloud.framework.events.EventBusException) UUID(java.util.UUID) ConnectException(java.net.ConnectException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with EventSubscriber

use of com.cloud.framework.events.EventSubscriber in project cosmic by MissionCriticalCloud.

the class RabbitMQEventBus method handleDelivery.

private void handleDelivery(final String queueName, final Envelope envelope, final byte[] body) {
    final Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
    if (queueDetails != null) {
        final EventSubscriber subscriber = queueDetails.third();
        final String routingKey = envelope.getRoutingKey();
        final String eventSource = getEventSourceFromRoutingKey(routingKey);
        final String eventCategory = getEventCategoryFromRoutingKey(routingKey);
        final String eventType = getEventTypeFromRoutingKey(routingKey);
        final String resourceType = getResourceTypeFromRoutingKey(routingKey);
        final String resourceUUID = getResourceUUIDFromRoutingKey(routingKey);
        final 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);
    }
}
Also used : EventSubscriber(com.cloud.framework.events.EventSubscriber) Channel(com.rabbitmq.client.Channel) Event(com.cloud.framework.events.Event)

Example 4 with EventSubscriber

use of com.cloud.framework.events.EventSubscriber in project cosmic by MissionCriticalCloud.

the class RabbitMQEventBus method stop.

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

Aggregations

EventSubscriber (com.cloud.framework.events.EventSubscriber)4 Channel (com.rabbitmq.client.Channel)4 IOException (java.io.IOException)3 EventBusException (com.cloud.framework.events.EventBusException)2 Event (com.cloud.framework.events.Event)1 AMQP (com.rabbitmq.client.AMQP)1 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)1 Connection (com.rabbitmq.client.Connection)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1 ConnectException (java.net.ConnectException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UUID (java.util.UUID)1 TimeoutException (java.util.concurrent.TimeoutException)1