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);
}
}
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;
}
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);
}
}
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;
}
Aggregations