EventListener multiple firing

Yanduganov Andrey August 3, 2017

Hi!

I use EventListerer and I have multiple fires for issueCreated event. Other events (e.g. issueDeleted) fires one time.

My code:

import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.event.JiraListener;
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.issue.IssueEventListener;
import com.atlassian.jira.issue.Issue;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

@Scanned
public class TestListener extends AbstractIssueEventListener implements InitializingBean, DisposableBean, IssueEventListener, JiraListener {
private static final Logger log = LoggerFactory.getLogger(TestListener.class);

@ComponentImport
private final EventPublisher eventPublisher;

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

@Override
public void issueCreated(IssueEvent issueEvent) {
Issue issue = issueEvent.getIssue();
System.out.println("Issue " + issue.getKey() + " was created");
}

@Override
public void issueDeleted(IssueEvent issueEvent) {
Issue issue = issueEvent.getIssue();
System.out.println("Issue " + issue.getKey() + " was deleted");
}

@Override
public void destroy() throws Exception {
// unregister ourselves with the EventPublisher
eventPublisher.unregister(this);
}

@Override
public void afterPropertiesSet() throws Exception {
// register ourselves with the EventPublisher
eventPublisher.register(this);
}
}

I also have tried to write this without extending AbstractIssueEventListener but I had the same result.

Have you any ideas about this?

P.S.

I have modified my code. I've added static variable that counts fires. Now my issueCreated looks like this:

Override
public void issueCreated(IssueEvent issueEvent) {
Issue issue = issueEvent.getIssue();
System.out.println("Issue " + issue.getKey() + " was created: " + ++cnt);
System.out.println("--------");
}

And logs looks like this:

[INFO] [talledLocalContainer] Issue TEST-64 was created
[INFO] [talledLocalContainer] Issue TEST-64 was created: 2
[INFO] [talledLocalContainer] --------
[INFO] [talledLocalContainer] Issue TEST-64 was created

I think this is a bug...

2 answers

0 votes
Gilles GAIDO July 19, 2019

my code on confluence 5.9.14 :

(be careful , do not register this class with @Named because it will be fired twice )

public class MyEventListener implements InitializingBean, DisposableBean {


@ConfluenceImport
private final EventPublisher eventPublisher;

@ConfluenceImport
private final SpaceManager spaceManager;

@Inject
public MyEventListener (final EventPublisher eventPublisher, final SpaceManager spaceManager) {
super();
this.eventPublisher = eventPublisher;
this.spaceManager = spaceManager;
}

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

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

@EventListener
public void onEvent(ConfluenceEvent event) {

if (event.getSource() instanceof MyListenedAction) {
final MyListenedAction action = (MyListenedAction) event.getSource();
doSomething(action );
}
}


}
0 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 4, 2017

It is likely that you have an old version of the listener registered in your JIRA. Make sure you implement the "destroy" method to make sure the listener is unregistered on removal of the plugin.

As for the old listener that is still running, you might have to delete it from DB or code. I haven't come across that scenario, so it is just a guess :)

Suggest an answer

Log in or Sign up to answer