EventListener is invoked 6 times for VersionCreateEvent. Why?

Hi,

I have created an event listener for VersionCreateEvent. On a new project, I have add a version (This is the first version). But the event listener is invoked 6 times. Tried a couple more times. Every time I add a version, the event listener is invoked 6 times. Does anyone know why this is happening or how to limit to just once?

@justin_shapiro

I have logged the event listener here:

[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.1 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO
[INFO] [talledLocalContainer]  ********  Version created : 1.1.3 *************
[INFO] [talledLocalContainer] List of child projects for this project: 
[INFO] [talledLocalContainer] CHON
[INFO] [talledLocalContainer] CHTWO

Thanks

Can you paste in your java code? More interested in the class declaration/construction and the methods (and their annotations). Feel free to the remove contents of the method.

1 Like

Here is the code @daniel:


import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.project.VersionCreateEvent;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;

import org.osgi.service.component.annotations.Component;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by kveluru on 4/12/17.
 */
@Scanned
@Component
public class VersionChangeListener extends AbstractIssueEventListener implements InitializingBean, DisposableBean{

    @ComponentImport
    private EventPublisher eventPublisher;

    @Autowired
    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()+" *************");

    }

}

That should be working. Couple of suggestions - Use Web Fragment Finder to see what the events are coming in and the data that they have (it’s got an event inspector in the configuration area).

Otherwise - check that the version object you’re being passed is from the same project ( version.getProjectId() ).

1 Like

@daniel Tried that. Unfortunately, they don’t have VersionCreateEvent listed under their Events configuration tab. :frowning:

@daniel Miraculously, it works fine now. Its being invoked just once. I didn’t make any changes. Don’t know what changed. Thanks anyway for your help.

hi I wonder if you still can reply to me,
I just copy you code into my plugin and restart my plugin, but. there still didn’t show any log, which meant my Listener doesn’t work,i don’t know why~~

oh now I made it!
I still wonder if I can use this plugin module to dynamically update the value of multi-select custom field??