EventListner for VersionCreateEvent

Hi,

I would like to create an event listener for VersionCreateEvent. Below is my code:

import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.event.project.VersionCreateEvent;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;


/**
 * Created by kveluru on 4/12/17.
 */
public class VersionChangeListener implements InitializingBean, DisposableBean{

    @ComponentImport
    private final EventPublisher eventPublisher;



    VersionChangeListener(EventPublisher eventPublisher){

        this.eventPublisher = eventPublisher;
    }

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

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


    @EventListener
    public void addVersionEvent(VersionCreateEvent versionCreateEvent){


        System.out.println(" ********  Version created : "+versionCreateEvent.getVersion()+" *************");
    }

}

I have created a version under project settings but I don’t see the log. I tried to debug but the break point at the system output never seems to be reached.

I have followed exactly everything from the tutorial: https://developer.atlassian.com/jiradev/jira-platform/guides/other/tutorial-writing-jira-event-listeners-with-the-atlassian-event-library

What am I doing wrong?

On further research, I found that the EventListeners have to be manually added to the Listeners in the Administration page.

When I tried to add the event listener I created, I got this error:

Exception loading class: [Class 'com.kaushik.jira.plugins.VersionChangeListener' is loadable from OSGi but no enabled plugins could autowire an instance.].

How do I solve this?

what’s your atlassain-plugin.xml look like? I’m thinking you want a component-import for eventPublisher and a component for your listener. something like:

<component-import key="eventPublisher" interface="com.atlassian.event.api.EventPublisher"/>
<component key="versionChangeListener" class="package,path.to.VersionChangeListener">

Hi Justin,

Thanks for your answer. I use annotations. So, atlassian-plugin.xml doesn’t have this info. But I have annotated eventPublisher as @ComponentImport and versionChangeListner as @Component .

Here is my code:

@Component
public class VersionChangeListener implements InitializingBean, DisposableBean{

    @ComponentImport
    private EventPublisher eventPublisher;


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

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


    @EventListener
    public void addVersionEvent(VersionCreateEvent versionCreateEvent){


        System.out.println(" ********  Version created : "+versionCreateEvent.getVersion()+" *************");

        //Get list of child projects.
        List<String> childProjectsList = new ArrayList<String>();
        childProjectsList.add("CHON");
        childProjectsList.add("CHTWO");



        //Iterate over each child project and update the version

        System.out.println("List of child projects for this project: ");
        for(String projectKey : childProjectsList){
            System.out.println(projectKey);
        }

    }

}

I get the same error after annotating them as well.

I don’t think annotations are gonna work. I don’t think there’s any component scanning. For fun maybe try the atlassian-plugin.xml.

1 Like

There you go. That’s what I was missing. I added @Scanned annotation and that fixed the problem.

From the error, I didn’t think it was a component scanning problem. Thanks for help Justin.

Hi Kaushik,

I am also having same problem, may i know where to ad @Scaneed in listener class.

Regards,
Suresh

Do we need to upload always through Admin page? may i know where do found this information?

I added @Scanned tag on top of @Component in the VersionChangeListener class.

Hi,
Another sample that work into my plugin, for a VersionReleaseEvent :

package fr.jira.appli.listener;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.event.project.VersionReleaseEvent;
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;
import com.atlassian.sal.api.lifecycle.LifecycleAware;

@ExportAsService
@Named
public class VersionReleaseListener implements LifecycleAware {

  private final EventPublisher eventPublisher;

  private static final Logger LOG = Logger.getLogger(VersionReleaseListener.class);

  @Inject
  public VersionReleaseListener(@JiraImport final EventPublisher eventPublisher) {
    this.eventPublisher = eventPublisher;
  }

  @Override
  public void onStart() {
    eventPublisher.register(this);
  }

  @Override
  public void onStop() {
    eventPublisher.unregister(this);
  }

  @EventListener
  public void addVersionEvent(final VersionReleaseEvent versionReleaseEvent) {

    LOG.info(" ********  Version release : " + versionReleaseEvent.getVersion() + " *************");

  }

}