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.
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.].
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:
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.