Trouble using EventListener in plugin

I am trying to implement a event listener to my Jira plugin. The steps I have done to try this were the following:

  1. Create a new plugin project with altas-create-jira-plugin
  2. Add the following dependencies to pom.xml:
        <dependency>
            <groupId>com.atlassian.event</groupId>
            <artifactId>atlassian-event</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>2.5.6.SEC02</version>
            <scope>provided</scope>
        </dependency>
  1. Added the following code in PluginListener.Java:
import com.atlassian.event.api.EventPublisher;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class PluginListener implements InitializingBean, DisposableBean {

    @ComponentImport
        private final EventPublisher eventPublisher;

        public PluginListener(EventPublisher eventPublisher) {
            this.eventPublisher = eventPublisher;
        }

        public void afterPropertiesSet() throws Exception {
            eventPublisher.register(this);
        }

   
    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }
}

When I try to run the plugin with atlas-run the plugin is disabled, and always times out if I try to re enable it. Am I doing things correctly here?

Thanks in advance,
Teddy

Hi @teddy.koker,

Have you tried following the tutorial from this?

Cheers,
Anne Calantog

Hey @acalantog,

Thanks for the help! I initially tried using that tutorial, but atlas-run would not work with <component> or <component-import> in the atlassian-plugin.xml file. The fix for this would seem to be using Spring Scanner annotations which can be seen above using @ComponentImport

Thank you for you time,
Teddy

Hi @teddy.koker,

Awesome! That makes sense. The tutorial is the old way of setting dependencies. Glad you were able to solve it on your own.

Cheers,
Anne Calantog

Hi, @teddy.koker.

Since you’re using annotations (and JIRA 7, I assume), try adding @Component above public class... and use @Inject in your constructor.

Cheers,
Ian

Thank you @iragudo and @acalantog,

I think I have solved most of my problems. The first thing I had to do was make the dependencies I added <scope>provided</scope>. Next I also had to add public void onIssueEvent(IssueEvent) to my Listener class for some reason I am not quite aware of.

Best,
Teddy

1 Like

@acalantog and @iragudo.

I am now trying to create an Issue type upon installation or enabling of my plugin. Here is my code:

@Component
public class PluginListener implements InitializingBean, DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(PluginListener.class);

    @ComponentImport
    private final EventPublisher eventPublisher;

    @ComponentImport
    private final IssueTypeManager issueTypeManager;

    @Inject
    public PluginListener(EventPublisher eventPublisher, IssueTypeManager issueTypeManager) {
        this.eventPublisher = eventPublisher;
        this.issueTypeManager = issueTypeManager;  works
    }

    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
        issueTypeManager.createIssueType("NewIssue", "This is the description", 0L); // This line only sometimes
        // ^^^
    }

    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }

    @EventListener
    public void onIssueEvent(IssueEvent issueEvent) {
         return;
    }
}

Once I add the above indicated line, Jira fails to even start after doing atlas-mvn clean and then altas-run. When I add the code to an initial jira plugin setup the plugin times out when I run atlas-run again. I can get it to work however if I disable the plugin in the manager while keeping it running, add the line of code, then enable the plugin. My question is twofold:

  1. What is the proper way to create an IssueType to be packaged with the plugin, making sure the type does not already exist?
  2. What is the best way to debug this stuff so I can stop bugging you all?

Thanks so much!
Teddy

  1. You should find issuetypes by name and try to locate yours. Then you should store the ID of the issuetype somewhere so you can more easily retrieve it, even if the user renames it.
  2. I find that it’s best to try and install the plugin at runtime to figure these issues out; When you install it at runtime the stdout should print the problem more deliberately. You’re likely getting an error right now, but it’s hidden in the jira startup logging as well.
    Often I simply upload the plugin again will show me the stacktrace I needed to fix my issue.

Hi Teddy,
In debugging, kindly check the logs for related stack traces (if any) or any error logs. Usually, they are informative enough to point us in the right direction.

Couple of things.

afterPropertiesSet Gets called the moment that the addon is initialized - the rest of the system might not be ready yet. Take a look at sal’s lifecycleaware - that should be triggered once the app and the system is ready.

As far as the event - make sure to annotate the class with @ExportAsService - event listeners has to be public components.

1 Like