How to Build an Annotation Based Event Listener for Atlassian Confluence Server
One rainy Monday morning, Rod called Ryan to his office. “Ryan, all Confluence pages that are created must have a label rod-is-great,” Rod said. “Sir, are you joking?” Ryan replied.
“No, I am not. Can you do it?” Rod said with a straight face. “I’ll get right on it sir,” Ryan said with a smirk as he left the office.
1. Gather the Tools
Ryan set up his tools:
2. Create the Event Listener
He executed the atlas-create-confluence-plugin
command in his terminal. Then Ryan typed com.javacodegeeks.example
when he was prompted for a groupId. For his artifactId, he placed event-listener
. In the version and package prompts, he just accepted the defaults by pressing ENTER. Finally, he pressed Y to accept everything.
3. Edit the Generated Files
Ryan deleted the files that he did not need from the generated skeleton files. His event-listner directory contained the following files:
pom.xml
README
LICENSE
- Under “src/main/resources” –
atlassian-plugin.xml
,event-listener.properties
, css folder, images folder, js folder, and META-INF folder (all folders contained their default files) - Under “src/main/java” –
com.javacodegeeks.example
package and under itPageLabeler.java
Ryan loves coding using his Eclipse IDE, so he executed atlas-mvn eclipse:eclipse
to create .classpath and .project. Now, he is able to import his Maven project into Eclipse. The project build errors of pom.xml
didn’t matter to Ryan because he is using the terminal to run the atlas-mvn
commands. He was just using Eclipse as a glorified editor.
4. Add the Logic
PageLabeler.java
package com.javacodegeeks.example; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.atlassian.confluence.event.events.content.page.PageCreateEvent; import com.atlassian.confluence.labels.LabelManager; import com.atlassian.confluence.pages.Page; import com.atlassian.confluence.util.LabelUtil; import com.atlassian.event.api.EventListener; import com.atlassian.event.api.EventPublisher; import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; @Component public class PageLabeler implements DisposableBean { private final EventPublisher eventPublisher; private final LabelManager labelManager; @Autowired public PageLabeler(@ComponentImport EventPublisher eventPublisher, @ComponentImport LabelManager labelManager) { this.eventPublisher = eventPublisher; this.labelManager = labelManager; eventPublisher.register(this); } @EventListener public void onPageCreateEvent(PageCreateEvent event) { Page page = event.getPage(); LabelUtil.addLabel("rod-is-great", labelManager, page); } @Override public void destroy() throws Exception { eventPublisher.unregister(this); } }
Annotation-based event listeners must be registered as a component for it to be used. The @Component
annotation also tells the Atlassian Spring Scanner that it’s a singleton class. The @Autowired
annotation marks the constructor as the default constructor for the class. The PageLabeler
is registered to the EventPublisher
during creation. It implements the DisposableBean
so that it will be unregistered when it is destroyed. When a PageCreateEvent
happens, the method annotated with @EventListener
is called. The label “rod-is-great” is added to the created page. See Event Listener Module for more details.
5. Test the Listener
Ryan executed atlas-run
to test his plug-in. He opened his browser and accessed http://localhost:1990/confluence. He signed in using admin
as his username and password. He created a blank page with the title “Ryan’s Bug Report” and clicked “Save”. His final output looks like this:
6. Summary
Ryan built his Event Listener by following these steps:
- Gather the Tools
- Create the Event Listener
- Edit the Generated Files
- Add the logic
- Test the Listener
7. Download the Source Code
This is an example of building an Annotation-Based Event Listener for the Atlassian Confluence Server.
You can download the source code of this example here: annotation-based-event-listener-atlassian-confluence-server.zip.