0

I am working on a BitBucket add-on that notifies the users of repository and branch events. While testing branch events I noticed that delete and create events are fired more than once (I am not listening to the BranchChangedEvent). From what I understand, the events are fired for the inherited classes as well and are handled by the two methods shown below.

@EventListener
public void onBranchCreatedEvent(BranchDeletedEvent event) {
    //do something
}

@EventListener
public void onBranchCreatedEvent(BranchCreatedEvent event) {
    //do something
}

Is there a good way to handle the two events in question without keeping track of what has already been handled? My requirement is to capture the name and the relative URL of the created/deleted branch.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dennis
  • 346
  • 3
  • 14

2 Answers2

0

To fix the problem, I kept @Component annotation at the class level as shown below and added component registration in the atlassian-plugin.xml file.

@Component
public class MyEventListener implements DisposableBean { 

    ... ctor with injected EventPublisher eventPublisher

    @EventListener
    public void onBranchCreatedEvent(BranchDeletedEvent event) {
        //do something
    }

    @EventListener
    public void onBranchCreatedEvent(BranchCreatedEvent event) {
        //do something
    }

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

atlassian-plugin.xml

 <component key="bitbucket-activity-listener" class="my.package.name.MyEventListener">
 </component>

Don't forget to remove Atlassian-Plugin-Key tag from your pom.xml under bitbucket-maven-plugin.

Hope this helps.

Dennis
  • 346
  • 3
  • 14
  • 1
    You don't not what version of Bitbucket Server you're dealing with, which makes it harder to help. You should _not_ need to do it this way, though. By removing the "Atlassian-Plugin-Key" from pom.xml you're switching from a pre-transformed plugin to a transformed one. That means your @Component annotation is _ignored_, and the only reason your listener is created is because of the you added in atlassian-plugin.xml. – Bryan Turner Jul 20 '17 at 23:30
0

I'm one of the Bitbucket Server developers. It's not clear how you're registering your event listener from the snippet you posted. It's also not clear how your add-on is packaged, and that's important too.

If you have a component (whether created using @Component and spring-scanner or created using a <component/> tag in atlassian-plugin.xml), you should never need to explicitly reference EventPublisher.register or unregister. Bitbucket Server includes a helper that detects @EventListener annotations as add-on components are being created/destroyed and automatically handles their registration/unregistration. That means if you use EventPublisher to manually register your listener instance it gets registered twice, and receives every event twice.

Another possibility, though, is that you're actually ending up with 2 instances of your listener being registered. You can typically "see" this easily in a debugger by putting a breakpoint in an @EventListener method and looking at System.identityHashCode(this) (where this is your listener instance) each time the breakpoint is hit. If you get more than one result from identityHashCode, it means you have multiple instances. (If you haven't overridden toString() on your listener you should also be able to use that to determine if you have multiple instances, since the default implementation includes the identity hashcode as hex characters.) I've helped other add-on developers fix issues where they had multiple instances of their listeners; for example, the stash2slack add-on. That thread might be useful to you too.

If you'd like to share some additional details about your add-on, I'd be happy to help investigate further.

Bryan Turner
  • 321
  • 3
  • 5