Annotation based Event Listener with component module

Available:

Confluence 3.3 and later

Status

DEPRECATED in favor of Event Listener with Atlassian Spring Scanner.

You can register your listener as component inside atlassian-plugin.xml.

You cannot use this approach with Atlassian Spring Scanner.

1
2
3
4
5
6
7
8
9
10
<atlassian-plugin key="listenerExample" name="Listener Examples" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="${project.organization.name}" url="${project.organization.url}" />
    </plugin-info>

    <component name="Annotated Event Listener" class="com.atlassian.confluence.plugin.example.listener.AnnotatedListener" key="annotatedListener"/>

</atlassian-plugin>

The methods must be annotated with the com.atlassian.event.api.EventListener annotation and only take a single parameter of the type of event this method is to service.

The event listener must also register itself with the EventPublisher which can be injected into the constructor. The event listener will need to implement the DisposableBean interface to unregister itself when the plugin is disabled or uninstalled.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.atlassian.confluence.plugin.example.listener;

import com.atlassian.confluence.event.events.security.LoginEvent;
import com.atlassian.confluence.event.events.security.LogoutEvent;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

public class AnnotatedListener implements DisposableBean{
    private static final Logger log = LoggerFactory.getLogger(AnnotatedListener.class);

    protected EventPublisher eventPublisher;

    public AnnotatedListener(EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
        eventPublisher.register(this);
    }

    @EventListener
    public void loginEvent(LoginEvent event) {
        log.error("Login Event: " + event);
    }

    @EventListener
    public void logoutEvent(LogoutEvent event) {
        log.error("Logout Event: " + event);
    }

    // Unregister the listener if the plugin is uninstalled or disabled.
    public void destroy() throws Exception
    {
        eventPublisher.unregister(this);
    }
}

INFO: An annotation-based event listener does not need to implement the com.atlassian.event.EventListener class.