Home » Software Development » Atlassian » How to Build an Annotation Based Event Listener for Atlassian Confluence Server

About Joel Patrick Llosa

I graduated from Silliman University in Dumaguete City with a degree in Bachelor of Science in Business Computer Application. I have contributed to many Java related projects at Neural Technologies Ltd., University of Southampton (iSolutions), Predictive Technologies, LLC., Confluence Service, North Concepts, Inc., NEC Telecom Software Philippines, Inc., and NEC Technologies Philippines, Inc. You can also find me in Upwork freelancing as a Java Developer.

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:

  1. Atlassian SDK
  2. Mars Eclipse

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 it PageLabeler.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:

how-to-build-an-annotation-based-event-listener-atlassian-confluence-server

Final Output

6. Summary

Ryan built his Event Listener by following these steps:

  1. Gather the Tools
  2. Create the Event Listener
  3. Edit the Generated Files
  4. Add the logic
  5. Test the Listener

7. Download the Source Code

This is an example of building an Annotation-Based Event Listener for the Atlassian Confluence Server.

Download
You can download the source code of this example here: annotation-based-event-listener-atlassian-confluence-server.zip.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments