use of org.apache.cloudstack.framework.messagebus.MessageHandler in project cloudstack by apache.
the class ServerEventHandlerImpl method defaultMessageHandler.
@MessageHandler(topic = ".*")
public void defaultMessageHandler(String subject, String topic, Object args) {
s_logger.info("DB Event Received - topic: " + topic + "; subject: " + subject);
org.apache.cloudstack.framework.events.Event event = (org.apache.cloudstack.framework.events.Event) args;
/* Method name should be on<ClassName><Operation> for example: onDomainCreate */
Method method = null;
try {
/* Only create event needs special implementation */
if (event.getEventType().contains("CREATE")) {
String methodName = "on" + event.getResourceType() + "Create";
method = _methodMap.get(methodName);
if (method == null) {
defaultCreateHandler(subject, topic, event);
} else {
method.invoke(this, subject, topic, event);
}
} else if (event.getEventType().contains("DELETE")) {
defaultDeleteHandler(subject, topic, event);
} else {
defaultHandler(subject, topic, event);
}
} catch (Exception e) {
s_logger.debug(e);
}
}
use of org.apache.cloudstack.framework.messagebus.MessageHandler 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";
Map<String, Object> cmdInfoObj = new HashMap<String, Object>();
if (info != null) {
Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> cmdInfo = ApiGsonHelper.getBuilder().create().fromJson(info, type);
cmdInfoObj.putAll(cmdInfo);
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");
}
String contextDetails = cmdInfo.get("ctxDetails");
if (contextDetails != null) {
Type objectMapType = new TypeToken<Map<Object, Object>>() {
}.getType();
Map<Object, Object> ctxDetails = ApiGsonHelper.getBuilder().create().fromJson(contextDetails, objectMapType);
cmdInfoObj.put("ctxDetails", ctxDetails);
}
}
// 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, Object> eventDescription = new HashMap<String, Object>();
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", ApiSerializerHelper.fromSerializedStringToMap(job.getResult()));
eventDescription.put("cmdInfo", cmdInfoObj);
eventDescription.put("status", "" + job.getStatus());
// If the event.accountinfo boolean value is set, get the human readable value for the username / domainname
if (UseEventAccountInfo.value()) {
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);
}
}
Aggregations