Search in sources :

Example 6 with EventBusException

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());
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) EventBusException(com.cloud.framework.events.EventBusException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) TimeoutException(java.util.concurrent.TimeoutException) ConfigurationException(javax.naming.ConfigurationException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) EventBusException(com.cloud.framework.events.EventBusException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 7 with EventBusException

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.");
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) CallContext(com.cloud.context.CallContext) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Date(java.util.Date) Project(com.cloud.projects.Project) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with EventBusException

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.");
    }
}
Also used : HashMap(java.util.HashMap) Zone(com.cloud.model.Zone) EventBus(com.cloud.framework.events.EventBus) HostPodVO(com.cloud.dc.HostPodVO) Date(java.util.Date) Event(com.cloud.framework.events.Event) Event(com.cloud.framework.events.Event) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with EventBusException

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.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) Date(java.util.Date) Snapshot(com.cloud.storage.Snapshot) Event(com.cloud.storage.Snapshot.Event) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

EventBusException (com.cloud.framework.events.EventBusException)9 EventBus (com.cloud.framework.events.EventBus)6 HashMap (java.util.HashMap)6 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 Channel (com.rabbitmq.client.Channel)3 IOException (java.io.IOException)3 Event (com.cloud.framework.events.Event)2 EventSubscriber (com.cloud.framework.events.EventSubscriber)2 Account (com.cloud.user.Account)2 User (com.cloud.user.User)2 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)2 Connection (com.rabbitmq.client.Connection)2 ConnectException (java.net.ConnectException)2 KeyManagementException (java.security.KeyManagementException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 TimeoutException (java.util.concurrent.TimeoutException)2 CallContext (com.cloud.context.CallContext)1 HostPodVO (com.cloud.dc.HostPodVO)1