Search in sources :

Example 6 with EventBusException

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

the class ApiServer method handleAsyncJobPublishEvent.

@MessageHandler(topic = AsyncJob.Topics.JOB_EVENT_PUBLISH)
public void handleAsyncJobPublishEvent(String subject, String senderAddress, Object args) {
    assert (args != null);
    @SuppressWarnings("unchecked") Pair<AsyncJob, String> eventInfo = (Pair<AsyncJob, String>) args;
    AsyncJob job = eventInfo.first();
    String jobEvent = eventInfo.second();
    if (s_logger.isTraceEnabled())
        s_logger.trace("Handle asyjob publish event " + jobEvent);
    EventBus eventBus = null;
    try {
        eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    if (!job.getDispatcher().equalsIgnoreCase("ApiAsyncJobDispatcher")) {
        return;
    }
    User userJobOwner = accountMgr.getUserIncludingRemoved(job.getUserId());
    Account jobOwner = accountMgr.getAccount(userJobOwner.getAccountId());
    // Get the event type from the cmdInfo json string
    String info = job.getCmdInfo();
    String cmdEventType = "unknown";
    if (info != null) {
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        Map<String, String> cmdInfo = ApiGsonHelper.getBuilder().create().fromJson(info, type);
        String eventTypeObj = cmdInfo.get("cmdEventType");
        if (eventTypeObj != null) {
            cmdEventType = eventTypeObj;
            if (s_logger.isDebugEnabled())
                s_logger.debug("Retrieved cmdEventType from job info: " + cmdEventType);
        } else {
            if (s_logger.isDebugEnabled())
                s_logger.debug("Unable to locate cmdEventType marker in job info. publish as unknown event");
        }
    }
    // For some reason, the instanceType / instanceId are not abstract, which means we may get null values.
    String instanceType = job.getInstanceType() != null ? job.getInstanceType() : "unknown";
    String instanceUuid = job.getInstanceId() != null ? ApiDBUtils.findJobInstanceUuid(job) : "";
    org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event("management-server", EventCategory.ASYNC_JOB_CHANGE_EVENT.getName(), jobEvent, instanceType, instanceUuid);
    Map<String, String> eventDescription = new HashMap<String, String>();
    eventDescription.put("command", job.getCmd());
    eventDescription.put("user", userJobOwner.getUuid());
    eventDescription.put("account", jobOwner.getUuid());
    eventDescription.put("processStatus", "" + job.getProcessStatus());
    eventDescription.put("resultCode", "" + job.getResultCode());
    eventDescription.put("instanceUuid", instanceUuid);
    eventDescription.put("instanceType", instanceType);
    eventDescription.put("commandEventType", cmdEventType);
    eventDescription.put("jobId", job.getUuid());
    eventDescription.put("jobResult", job.getResult());
    eventDescription.put("cmdInfo", job.getCmdInfo());
    eventDescription.put("status", "" + job.getStatus());
    // If the event.accountinfo boolean value is set, get the human readable value for the username / domainname
    Map<String, String> configs = configDao.getConfiguration("management-server", new HashMap<String, String>());
    if (Boolean.valueOf(configs.get("event.accountinfo"))) {
        DomainVO domain = domainDao.findById(jobOwner.getDomainId());
        eventDescription.put("username", userJobOwner.getUsername());
        eventDescription.put("accountname", jobOwner.getAccountName());
        eventDescription.put("domainname", domain.getName());
    }
    event.setDescription(eventDescription);
    try {
        eventBus.publish(event);
    } catch (EventBusException evx) {
        String errMsg = "Failed to publish async job event on the the event bus.";
        s_logger.warn(errMsg, evx);
    }
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) User(com.cloud.user.User) HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) AsyncJob(org.apache.cloudstack.framework.jobs.AsyncJob) DomainVO(com.cloud.domain.DomainVO) Type(java.lang.reflect.Type) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.cloud.utils.Pair) NameValuePair(org.apache.http.NameValuePair) MessageHandler(org.apache.cloudstack.framework.messagebus.MessageHandler)

Example 7 with EventBusException

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

the class RabbitMQEventBus method publish.

// publish event on to the exchange created on AMQP server
@Override
public void publish(Event event) throws EventBusException {
    String routingKey = createRoutingKey(event);
    String eventDescription = event.getDescription();
    try {
        Connection connection = getConnection();
        Channel channel = createChannel(connection);
        createExchange(channel, amqpExchangeName);
        publishEventToExchange(channel, amqpExchangeName, routingKey, eventDescription);
        channel.close();
    } catch (AlreadyClosedException e) {
        closeConnection();
        throw new EventBusException("Failed to publish event to message broker as connection to AMQP broker in lost");
    } catch (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(org.apache.cloudstack.framework.events.EventBusException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) 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)

Example 8 with EventBusException

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

the class AlertGenerator method publishAlertOnEventBus.

public static void publishAlertOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) {
    String configKey = Config.PublishAlertEvent.key();
    String value = s_configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.ALERT_EVENT.getName(), alertType, null, null);
    Map<String, String> eventDescription = new HashMap<String, String>();
    DataCenterVO dc = s_dcDao.findById(dataCenterId);
    HostPodVO pod = s_podDao.findById(podId);
    eventDescription.put("event", alertType);
    if (dc != null) {
        eventDescription.put("dataCenterId", dc.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);
    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 (EventBusException e) {
        s_logger.warn("Failed to publish alert on the the event bus.");
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) HostPodVO(com.cloud.dc.HostPodVO) Date(java.util.Date) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with EventBusException

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

the class SnapshotStateListener method pubishOnEventBus.

private void pubishOnEventBus(String event, String status, Snapshot vo, State oldState, State newState) {
    String configKey = Config.PublishResourceStateEvent.key();
    String value = s_configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    String resourceName = getEntityFromClassName(Snapshot.class.getName());
    org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
    Map<String, String> eventDescription = new HashMap<String, String>();
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("old-state", oldState.name());
    eventDescription.put("new-state", newState.name());
    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 (EventBusException e) {
        s_logger.warn("Failed to publish state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) Date(java.util.Date) Snapshot(com.cloud.storage.Snapshot) Event(com.cloud.storage.Snapshot.Event) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 10 with EventBusException

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

the class VolumeStateListener method pubishOnEventBus.

private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
    String configKey = Config.PublishResourceStateEvent.key();
    String value = _configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    String resourceName = getEntityFromClassName(Volume.class.getName());
    org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
    Map<String, String> eventDescription = new HashMap<String, String>();
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("old-state", oldState.name());
    eventDescription.put("new-state", newState.name());
    String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
    eventDescription.put("eventDateTime", eventDate);
    eventMsg.setDescription(eventDescription);
    try {
        s_eventBus.publish(eventMsg);
    } catch (EventBusException e) {
        s_logger.warn("Failed to state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) Date(java.util.Date) Volume(com.cloud.storage.Volume) Event(com.cloud.storage.Volume.Event) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

EventBusException (org.apache.cloudstack.framework.events.EventBusException)11 HashMap (java.util.HashMap)7 EventBus (org.apache.cloudstack.framework.events.EventBus)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)7 SimpleDateFormat (java.text.SimpleDateFormat)6 Date (java.util.Date)6 Account (com.cloud.user.Account)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)3 Channel (com.rabbitmq.client.Channel)3 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)3 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 ConfigurationException (javax.naming.ConfigurationException)3 EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)3 DataCenterVO (com.cloud.dc.DataCenterVO)2 User (com.cloud.user.User)2 Connection (com.rabbitmq.client.Connection)2 UUID (java.util.UUID)2 Event (org.apache.cloudstack.framework.events.Event)2