EventListener Example

Available:

Confluence 1.4 and later

Changed:

In Confluence 3.3 and later, Confluence events are annotation-based. This means that you have two options when writing event listeners. Option 1 is the Event Listener plugin module, using the com.atlassian.event.EventListener class. Option 2 is to declare your listener as a component and use annotation-based event listeners, annotating your methods to be called for specific event types. The component will need to register itself at the time it gets access to the EventPublisher, typically in the constructor. More information and examples are in Event Listener Module.

This page gives the example code for an event listener, as described in the page about event listener plugins.

Below is an example of an EventListener that listens for the LoginEvent and LogoutEvent.

package com.atlassian.confluence.extra.userlister;

import com.atlassian.confluence.event.events.security.LoginEvent;
import com.atlassian.confluence.event.events.security.LogoutEvent;
import com.atlassian.event.Event;
import com.atlassian.event.EventListener;
import org.apache.log4j.Logger;

public class UserListener implements EventListener {
    private static final Logger log = Logger.getLogger(UserListener.class);

    private Class[] handledClasses = new Class[]{ LoginEvent.class, LogoutEvent.class};


    public void handleEvent(Event event) {
        if (event instanceof LoginEvent) {
            LoginEvent loginEvent = (LoginEvent) event;
            log.info(loginEvent.getUsername() + " logged in (" +  loginEvent.getSessionId() + ")");
        } else if (event instanceof LogoutEvent) {
            LogoutEvent logoutEvent = (LogoutEvent) event;
            log.info(logoutEvent.getUsername() + " logged out (" +  logoutEvent.getSessionId() + ")");
        }
}

    public Class[] getHandledEventClasses() {
        return handledClasses;
    }
    
}
Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport