Available: | Confluence 1.4 and later |
Legacy: | In Confluence 3.3 and later, the preferred approach is to use annotation-based events with Atlassian Spring Scanner. More information and examples are on Event Listener Module page. |
For an introduction to event listener plugin modules, read Event Listener module.
The easiest thing here is to consult the Javadoc,
in the com.atlassian.confluence.event.events package. When you implement an EventListener
you will provide an array of
Class objects which represent the events you wish to handle.
The naming of most events are self explanatory (for example, GlobalSettingsChangedEvent
or ReindexStartedEvent
).
However, there are some that need further clarification.
Event Class | Published |
---|---|
Thrown when a label is created and comes into existence. | |
Thrown when a label is disassociated from a piece of content (as opposed to itself being removed). | |
Thrown when an existing label is associated with a piece of content (as opposed to being newly created). | |
Thrown when a label is removed (as opposed to being disassociated from a piece of content). |
EventListener
The EventListener
interface defines two methods that must be implemented: getHandledEventClasses()
and handleEvent()
.
Implement getHandledEventClasses()
The getHandledEventClasses()
method holds an array of class objects representing the events you wish to listen for.
getHandledEventClasses()
.So, if you want your listener to receive only SpaceCreatedEvent
and SpaceRemovedEvent
, do the following:
1 2private static final Class[] HANDLED_EVENTS = new Class[] { SpaceCreateEvent.class, SpaceRemovedEvent.class }; public Class[] getHandledEventClasses() { return HANDLED_EVENTS; }
Alternatively, to receive all possible events, use the following code:
1 2/** * Returns an empty array, thereby handling every ConfluenceEvent * @return */ public Class[] getHandledEventClasses() { return new Class[0]; }
Implement handleEvent()
The following implementation simply relies upon the toString()
implementation of the event and logs it to a log4j appender.
1 2public void handleEvent(Event event) { if (!initialized) initializeLogger(); log.info(event); }
Most often, a handleEvent(..)
method will type check each event sent through it and execute some conditional logic.
1 2public void handleEvent(Event event) { if (event instanceof LoginEvent) { LoginEvent loginEvent = (LoginEvent) event; // ... logic associated with the LoginEvent } else if (event instanceof LogoutEvent) { LogoutEvent logoutEvent = (LogoutEvent) event; // ... logic associated with the LogoutEvent } }
You can find a full example of an EventListener
class that listens for login and logout events in EventListener example.
EventListener
as a module to your pluginEventListener
as a module, create an atlassian-plugin.xml
file.The atlassian-plugin.xml
file has been described here in detail.
This is an example of a listener plugin module included in an atlassian-plugin.xml
file.
1 2<atlassian-plugin name='Optional Listeners' key='confluence.extra.auditor'> <plugin-info> <description>Audit Logging</description> <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/> <version>1.0</version> </plugin-info> <listener name='Audit Log Listener' class='com.atlassian.confluence.extra.auditer.AuditListener' key='auditListener'> <description>Provides an audit log for each event within Confluence.</description> </listener> </atlassian-plugin>
Rate this page: