ProjectDeletedEvent

Have a scenario where I need to handle a project getting deleted in JIRA.

 @EventListener
    public void onProjectDeletedEvent(ProjectDeletedEvent projectDeletedEvent) {
        projectDeletedEvent.getProject();
    }

Had something written down like so. Which is very similar to what I have done for list of others.

Trouble is above code never gets called when a project gets deleted. Similar code in JIRA source works perfectly… I’m scratching my head as to why.

Any help is sincerely appreciated :slight_smile:

Cheers,
Parthi

1 Like

You need to register your class with the EventPublisher: https://docs.atlassian.com/DAC/javadoc/events/2.1.1/reference/com/atlassian/event/api/EventPublisher.html

You should create a a component which implements InitializingBean and add the registration to the afterPropertiesSet to make sure it is executed once the bean has been initialised (which is a good way to know for sure Jira is ready).

Semi-pseudo code:

public class EventListener implements InitializingBean {
  public EventListener(EventPublisher eventPublisher) {
    this.eventPublisher = eventPublisher;
  }

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

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

Thanks for that Remie.

Yes I did try the other usual listener route as well like you would if you are writing a event listener for Issue events.

This is the confusing part for me.

I have 3, Issue,Groups and Project. First 2 absolutely no issues they just work. Project event listener :frowning: just simply does not what it says on the tin no matter what route I follow.

Strangely this works.

@Scanned
@Component
public class ProjectEventListener implements InitializingBean, DisposableBean {

    private final EventPublisher eventPublisher;

    @Inject
    public ProjectEventListener(EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

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

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

    @EventListener
    public void onProjectDeletedEvent(ProjectDeletedEvent projectDeletedEvent) {
        System.out.println("project deleted" + projectDeletedEvent);        
   }
}

@Component seems to be the missing link :slight_smile:

Strangely I don’t need that for a Listener that is listening to events related to Group updates :roll_eyes: no idea why this one needs it … yay but its working now

Thanks

@i0in.ltd yeah I was wondering if I need to further explain the “You should create a a component” part of my comment :slight_smile:

For some reason, the listener that is listening to events related to Group is picked up by the Spring scanner. Or maybe you have added it as <component /> to the atlassian-plugin.xml?

Anyway, glad to hear that it is working!

2 Likes

Thats the part I don’t understand. Nothing is defined on the Group event listener to say its a Component

its POJO which has methods declared like so

 @com.atlassian.event.api.EventListener
    public void onGroupCreatedEvent(GroupCreatedEvent groupCreatedEvent) {
        //do something 
    }

Yep I’m glad its working now I can perform the required cleanups when a project is deleted.

Thanks again Remie for spending some time on this.

Cheers,
Parthi

1 Like