Bamboo 2.2 : Building a Notification Plugin
This page last changed on Mar 09, 2009 by bmccoy.
Below I have tried to outline the concepts involved in creating a notification plugin. To create a notification plugin you will need to create the following classes: Notification Components OverviewWhilst there are 3 classes to be created there are other components which work together to send the notification.
Notification TypeThe NotificationType object identifies your custom notification. Users will be able to select this from the drop down menu and provide any configuration information you require. Notification Rule Event ListenerThe Event Listener will listen for an event to be fired in Bamboo which you would like to cause a notification. These events are predefined in Bamboo. You can find a list of available events as well as more information on implementing and event listener in the Bamboo Event Listener Module documentation. A notification event listener is responsible for retrieving the Notification Rules from the database, filtering them (determining which will/wont be sent) and creating a Notification object containing the recipients that will be receiving the notification. This Notification object then needs to be passed to the NotificationDispatcher for processing. Context information is available in the event object and you can inject Managers to obtain any other required information. Example Listener Class public class BuildCompletedNotificationListener implements HibernateEventListener { private NotificationManager notificationManager; private NotificationDispatcher notificationDispatcher; private BuildManager buildManager; public Class[] getHandledEventClasses() { Class[] array = {BuildCompletedEvent.class}; return array; } public void handleEvent(Event event) { BuildCompletedEvent buildEvent = (BuildCompletedEvent) event; Build build = buildManager.getBuildByKey(buildEvent.getBuildPlanKey()); Notification myNotification = new MyCustomNotification(); myNotification.setEvent(event); Set<NotificationRule> rules = notificationManager.getNotificationRules(build); for (NotificationRule rule : rules) { NotificationType notificationType = rule.getNotificationType(); if (notificationType instanceof MyCustomNotificationType) { if (notificationType.isNotificationRequired(event)) { NotificationRecipient recipient = rule.getNotificationRecipient(); myNotification.addRecipient(recipient); } } } notificationDispatcher.dispatchNotifications(myNotification); } public void setNotificationManager(NotificationManager notificationManager) { this.notificationManager = notificationManager; } public void setNotificationDispatcher(NotificationDispatcher notificationDispatcher) { this.notificationDispatcher = notificationDispatcher; } public void setBuildManager(BuildManager buildManager) { this.buildManager = buildManager; } } NotificationThe Notification class is a self contained object which is used to dispatch the required notifications. You are not required to register your Notification object as a plugin module as it will be instantiated by your own Event Listener. Notifications must implement the com.atlassian.bamboo.notification.Notification interface. There is an abstract class com.atlassian.bamboo.notification.AbstractNotification which you can extend that implements a lot of these methods for you. A Notification will contain any context information which you will need to generate content (usually the event), a list of recipients to send the notification to and a list of recipients to exclude. The notification class is responsible for generating the content (body) of the notifications. There is no specific generate the content of your notifications, we user Freemarker for our notifications as the templates are customisable outside of the application. Example Content Generation @Nullable public String getHtmlEmailContent() throws Exception { Event event = getEvent(); if (event != null) { Map<String, Object> context = new HashMap<String, Object>(); populateContext(context); // put any information the context which you want available in your // freemarker template e.g. the build, buildResultsSummary etc try { return templateRenderer.render("notification-templates/BuildCompletedTextEmail.ftl", context); } catch (Exception e) { log.error("Could not render email content", e); return null; } } else { log.error("Event is null, could not create Email content for " + getDescription()); return null; } } Advanced Notification Interface The Notification DispatcherOnce you send your Notification object off to the dispatcher it will do the following:
Notification Recipient Notification Transport Questions?Why do I have excluded recipients? For example, the build-commented notification we do not want to get sent to the user who actually created the comment. We would add that user to the list, and when the groups get evaluated this user would get excluded but all other members would still receive the notification. Can I just provide html content and not text content? What is a multipart email? |
![]() |
Document generated by Confluence on Mar 09, 2009 17:07 |