Introduction

In ACS 4.1 notion of event bus is introduced in CloudStack to let components internal to CloudStack and external components publish-and-subscribe to the CloudStack generated events. At present there is a plug-in (plugins/event-bus/rabbitmq/) which relies on the AMQP compliant server acting as middleware to publish and subscribe to the events. While it lets external components outside the CloudStack to subscribe to events, there is need for CloudStack components (service, plug-ins etc)  to both publish and subscribe events. In scenarios only components that publish and subscribe to events are components in the management server, then requirement to have external AMQP server as dependency in undesirable. So goal of this document is to present in-memory event bus that will let CloudStack components to both publish and subscribe to the events.

 

A new plug-in "plugins/event-bus/inmemory" is added to CS to enable event bus. To enable inmemory event bus in dev environment create a bean config file "server/resources/META-INF/cloudstack/core/spring-event-bus-context.xml"

 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                      http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context-3.0.xsd"
                      >

<bean id="eventNotificationBus" class="org.apache.cloudstack.mom.inmemory.InMemoryEventBus">
<property name="name" value="eventNotificationBus"/>
</bean>

</beans>

 

In build based environment place the file in class path, spring initialisation will pick the file and initialise the bean.

 

Publishing an event

 

To publish an event your component can do as described below.

protected static EventBus s_eventBus = ComponentContext.getComponent(EventBus.class);

org.apache.cloudstack.framework.events.Event event = new Event(....define the params as required....)

s_eventBus.publish(event);

 

Subscribing to events

 

To subscribe to events your component can do as described below.

 

Implement interface EventSubscriber where 'onEvent' is callback function that receives callback on an event occurs. To subscribe to the events, create the 'EventTopic' that defined the events that your component is interested in subscribing to and register with event bus.

For e.g if your component is interested in subscribing to all the action events corresponding to VM resource then you would create EventTopic as below and subscribe with the event bus.

 

public class VmActionEventHandler implements EventSubscriber {

    @Override
    public void onEvent(Event event) {

        // handle the event here
    }
}

 

EventTopic topic = new EventTopic(EventCategory.ACTION_EVENT.getName(), VirtualMachine.class.getName(), null, null, null);

_eventBus.subscribe(topic, new VmActionEventHandler());

 

 

 

 

 

 

 

 

  • No labels