mirror of https://github.com/apache/cloudstack.git
framework to publish and subscribe events
This commit is contained in:
parent
5ae868d0f9
commit
967289d8c5
|
|
@ -0,0 +1,30 @@
|
|||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cloud-framework-events</artifactId>
|
||||
<name>Apache CloudStack Event Notification Framework</name>
|
||||
<parent>
|
||||
<groupId>org.apache.cloudstack</groupId>
|
||||
<artifactId>cloudstack</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
import com.cloud.utils.component.Adapter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Publish and Subscribe provider interface
|
||||
*
|
||||
*/
|
||||
public interface EventBus extends Adapter{
|
||||
/**
|
||||
* Publish an event
|
||||
*
|
||||
* @param category category of the event being published (e.g. action, usage, alert etc)
|
||||
* @param type type of the event (e.g. vm stop, volume delete etc)
|
||||
* @param description description of the event
|
||||
* @return true if the event has been successfully published.
|
||||
*/
|
||||
boolean publish(String category, String type, Map<String, String> description);
|
||||
|
||||
/**
|
||||
* Subscribe to events of a category and a type
|
||||
*
|
||||
* @param category category of the event being subscribed (e.g. action, usage, alert etc)
|
||||
* @param type type of the event (e.g. vm stop, volume delete etc)
|
||||
* @param subscriber class that is intends to receive subscribed event
|
||||
* @return true if the subscribe has been successfully registered.
|
||||
*/
|
||||
boolean subscribe(String category, String type, EventSubscriber subscriber);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
public class EventCategory {
|
||||
public static final String ACTION_EVENT = "Action Event";
|
||||
public static final String USAGE_EVENT = "Usage Event";
|
||||
public static final String ALERT_EVENT = "Alert Event";
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.cloud.utils.component.Adapters;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.component.AnnotationInterceptor;
|
||||
|
||||
import net.sf.cglib.proxy.Callback;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
|
||||
public class EventPublisher implements MethodInterceptor, AnnotationInterceptor<Publish> {
|
||||
|
||||
private static EventBus _eventBus = null;
|
||||
|
||||
@Override
|
||||
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||
Publish event = interceptStart(method);
|
||||
boolean success = true;
|
||||
try {
|
||||
return methodProxy.invokeSuper(object, args);
|
||||
} catch (Exception e){
|
||||
success = false;
|
||||
interceptException(method, event);
|
||||
throw e;
|
||||
} finally {
|
||||
if(success){
|
||||
interceptComplete(method, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needToIntercept(AnnotatedElement element) {
|
||||
if (!(element instanceof Method)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
Method method = (Method)element;
|
||||
Publish event = method.getAnnotation(Publish.class);
|
||||
if (event != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publish interceptStart(AnnotatedElement element) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interceptComplete(AnnotatedElement element, Publish event) {
|
||||
_eventBus = getEventBus();
|
||||
if (_eventBus != null) {
|
||||
Map<String, String> description = new HashMap<String, String>();
|
||||
description.put("description", event.eventDescription());
|
||||
_eventBus.publish(event.eventCategory(), event.eventType(), description);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interceptException(AnnotatedElement element, Publish attach) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Callback getCallback() {
|
||||
return this;
|
||||
}
|
||||
|
||||
private EventBus getEventBus() {
|
||||
if (_eventBus == null) {
|
||||
ComponentLocator locator = ComponentLocator.getLocator("management-server");
|
||||
Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||
if (eventBusImpls != null) {
|
||||
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||
_eventBus = eventBusenum.nextElement();
|
||||
}
|
||||
}
|
||||
return _eventBus;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface EventSubscriber {
|
||||
|
||||
/**
|
||||
* Callback method. EventBus calls this method on occurrence of subscribed event
|
||||
*
|
||||
* @param category category of the event being subscribed (e.g. action, usage, alert etc)
|
||||
* @param type type of the event (e.g. vm stop, volume delete etc)
|
||||
* @param description description of the event
|
||||
*/
|
||||
void recieve(String category, String type, Map<String, String> description);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ TYPE, METHOD })
|
||||
@Retention(RUNTIME)
|
||||
public @interface Publish {
|
||||
|
||||
String eventCategory();
|
||||
|
||||
String eventType();
|
||||
|
||||
String eventDescription();
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.apache.cloudstack.framework.events;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({METHOD })
|
||||
@Retention(RUNTIME)
|
||||
public @interface Subscribe {
|
||||
|
||||
String eventCategory();
|
||||
|
||||
String eventType();
|
||||
}
|
||||
1
pom.xml
1
pom.xml
|
|
@ -157,6 +157,7 @@
|
|||
<module>awsapi</module>
|
||||
<module>patches</module>
|
||||
<module>test</module>
|
||||
<module>framework/events</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import com.cloud.event.ActionEventCallback;
|
|||
import com.cloud.utils.component.AnnotationInterceptor;
|
||||
import com.cloud.utils.component.InterceptorLibrary;
|
||||
import com.cloud.utils.db.DatabaseCallback;
|
||||
import org.apache.cloudstack.framework.events.EventPublisher;
|
||||
|
||||
public class DefaultInterceptorLibrary implements InterceptorLibrary {
|
||||
|
||||
|
|
@ -29,5 +30,6 @@ public class DefaultInterceptorLibrary implements InterceptorLibrary {
|
|||
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
|
||||
interceptors.add(new DatabaseCallback());
|
||||
interceptors.add(new ActionEventCallback());
|
||||
interceptors.add(new EventPublisher());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue