use of com.cloud.framework.events.EventBusException in project cosmic by MissionCriticalCloud.
the class RabbitMQEventBus method publish.
// publish event on to the exchange created on AMQP server
@Override
public void publish(final Event event) throws EventBusException {
String routingKey = createRoutingKey(event);
final String eventDescription = event.getDescription();
try {
final Connection connection = getConnection();
final Channel channel = createChannel(connection);
createExchange(channel, amqpExchangeName);
if (!amqpQueueName.isEmpty()) {
createQueue(channel, amqpQueueName);
bindQueue(channel, amqpQueueName, amqpExchangeName);
routingKey = amqpQueueName;
}
publishEventToExchange(channel, amqpExchangeName, routingKey, eventDescription);
channel.close();
} catch (final AlreadyClosedException e) {
closeConnection();
throw new EventBusException("Failed to publish event to message broker as connection to AMQP broker in lost");
} catch (final Exception e) {
throw new EventBusException("Failed to publish event to message broker due to " + e.getMessage());
}
}
use of com.cloud.framework.events.EventBusException in project cosmic by MissionCriticalCloud.
the class ActionEventUtils method publishOnEventBus.
private static void publishOnEventBus(final long userId, final long accountId, final String eventCategory, final String eventType, final Event.State state, final String description) {
final String configKey = Config.PublishActionEvent.key();
final String value = s_configDao.getValue(configKey);
final boolean configValue = Boolean.parseBoolean(value);
if (!configValue) {
return;
}
try {
s_eventBus = ComponentContext.getComponent(EventBus.class);
} catch (final NoSuchBeanDefinitionException nbe) {
// no provider is configured to provide events bus, so just return
return;
}
// get the entity details for which ActionEvent is generated
String entityType = null;
String entityUuid = null;
final CallContext context = CallContext.current();
// Get entity Class(Example - VirtualMachine.class) from the event Type eg. - VM.CREATE
final Class<?> entityClass = EventTypes.getEntityClassForEvent(eventType);
if (entityClass != null) {
// Get uuid from id
final Object param = context.getContextParameter(entityClass);
if (param != null) {
try {
entityUuid = getEntityUuid(entityClass, param);
entityType = entityClass.getName();
} catch (final Exception e) {
s_logger.debug("Caught exception while finding entityUUID, moving on");
}
}
}
final com.cloud.framework.events.Event event = new com.cloud.framework.events.Event(ManagementService.Name, eventCategory, eventType, EventTypes.getEntityForEvent(eventType), entityUuid);
final Map<String, String> eventDescription = new HashMap<>();
final Project project = s_projectDao.findByProjectAccountId(accountId);
final Account account = s_accountDao.findById(accountId);
final User user = s_userDao.findById(userId);
// if account has been deleted, this might be called during cleanup of resources and results in null pointer
if (account == null) {
return;
}
if (user == null) {
return;
}
if (project != null) {
eventDescription.put("project", project.getUuid());
}
eventDescription.put("user", user.getUuid());
eventDescription.put("account", account.getUuid());
eventDescription.put("event", eventType);
eventDescription.put("status", state.toString());
eventDescription.put("entity", entityType);
eventDescription.put("entityuuid", entityUuid);
// Put all the first class entities that are touched during the action. For now atleast put in the vmid.
populateFirstClassEntities(eventDescription);
eventDescription.put("description", description);
final String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
eventDescription.put("eventDateTime", eventDate);
event.setDescription(eventDescription);
try {
s_eventBus.publish(event);
} catch (final EventBusException e) {
s_logger.warn("Failed to publish action event on the the event bus.");
}
}
use of com.cloud.framework.events.EventBusException in project cosmic by MissionCriticalCloud.
the class AlertGenerator method publishAlertOnEventBus.
public static void publishAlertOnEventBus(final String alertType, final long dataCenterId, final Long podId, final String subject, final String body) {
final String configKey = Config.PublishAlertEvent.key();
final String value = s_configDao.getValue(configKey);
final boolean configValue = Boolean.parseBoolean(value);
if (!configValue) {
return;
}
try {
s_eventBus = ComponentContext.getComponent(EventBus.class);
} catch (final NoSuchBeanDefinitionException nbe) {
// no provider is configured to provide events bus, so just return
return;
}
final com.cloud.framework.events.Event event = new Event(ManagementService.Name, EventCategory.ALERT_EVENT.getName(), alertType, null, null);
final Map<String, String> eventDescription = new HashMap<>();
final Zone zone = s_zoneRepository.findOne(dataCenterId);
final HostPodVO pod = s_podDao.findById(podId);
eventDescription.put("event", alertType);
if (zone != null) {
eventDescription.put("dataCenterId", zone.getUuid());
} else {
eventDescription.put("dataCenterId", null);
}
if (pod != null) {
eventDescription.put("podId", pod.getUuid());
} else {
eventDescription.put("podId", null);
}
eventDescription.put("subject", subject);
eventDescription.put("body", body);
final String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
eventDescription.put("eventDateTime", eventDate);
event.setDescription(eventDescription);
try {
s_eventBus.publish(event);
} catch (final EventBusException e) {
s_logger.warn("Failed to publish alert on the the event bus.");
}
}
use of com.cloud.framework.events.EventBusException in project cosmic by MissionCriticalCloud.
the class SnapshotStateListener method pubishOnEventBus.
private void pubishOnEventBus(final String event, final String status, final Snapshot vo, final State oldState, final State newState) {
final String configKey = Config.PublishResourceStateEvent.key();
final String value = s_configDao.getValue(configKey);
final boolean configValue = Boolean.parseBoolean(value);
if (!configValue) {
return;
}
try {
s_eventBus = ComponentContext.getComponent(EventBus.class);
} catch (final NoSuchBeanDefinitionException nbe) {
// no provider is configured to provide events bus, so just return
return;
}
final String resourceName = getEntityFromClassName(Snapshot.class.getName());
final com.cloud.framework.events.Event eventMsg = new com.cloud.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
final Map<String, String> eventDescription = new HashMap<>();
eventDescription.put("resource", resourceName);
eventDescription.put("id", vo.getUuid());
eventDescription.put("old-state", oldState.name());
eventDescription.put("new-state", newState.name());
final String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
eventDescription.put("eventDateTime", eventDate);
eventMsg.setDescription(eventDescription);
try {
s_eventBus.publish(eventMsg);
} catch (final EventBusException e) {
s_logger.warn("Failed to publish state change event on the the event bus.");
}
}
Aggregations