Event Listener module
Job module
Language module
Macro Module
Servlet Filter module
Servlet module
Theme module
Web UI modules
Workflow module

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.

1
2
<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
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.

Rate this page: