2016. szeptember 21., szerda

Creating event listener for JIRA issue


Issue update event listener makes it possible to when the issue is created or modified in JIRA. As the event listener is registered to the lifecycle of the issue, the listener will be notified independently of the actual GUI element, used to create or update the issue.

Also the event listener will be notified after

  • new issue has been created 
  • issue has been modified via the edit issue popup
  • editing the issue directly on the display issue page
  • modifying the issue by sour application
  • modifying the issue via REST API
You need to implement the listener class. You don't need to implement any particular interface, but create following methods:
  • event handler method, marked with the @EventListener annotation.
  • method for registering the listener, marked with the @PostConstruct annotation.
  • method for unregistering the listener, marked with the @PreDestroy annotation.


package at.a1ta.eap.tasktrack.jira.ppm.fieldsync.eventlistener;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;

/**
 * Issue update event listener. Makes it possible to handle event when the issue is created or modified in JIRA.
 * 
 * @author Peter Varga
 *
 */
public class IssueListener {
 // Logger instance
 private static final Logger log = LoggerFactory.getLogger(IssueListener.class);

 @Autowired
 private EventPublisher eventPublisher;

 @PostConstruct
 private void init() {
  eventPublisher.register(this);
  log.debug("at.a1ta.eap.tasktrack.jira.ppm.fieldsync.eventlistener.IssueListener registered to EventPublisher");
 }

 @PreDestroy
 private void preDestroy() {
  eventPublisher.unregister(this);
  log.debug("at.a1ta.eap.tasktrack.jira.ppm.fieldsync.eventlistener.IssueListener unregistered form EventPublisher");
 }

 @EventListener
 public void onIssueEvent(IssueEvent issueEvent) {
  Long eventTypeId = issueEvent.getEventTypeId();
  if (!isEventTypeToBehandled(eventTypeId)) {
   return;
  }

  Issue issue = issueEvent.getIssue();

  log.info("Issue changed {}", issue);

 }

 /**
  * Answers if the event type needs to be handled by this listener. IN this case when issue created or updated. For more event types, refer to the
  * {@link EventType} class.
  */
 private boolean isEventTypeToBehandled(Long eventTypeId) {
  return eventTypeId.equals(EventType.ISSUE_CREATED_ID) || eventTypeId.equals(EventType.ISSUE_UPDATED_ID);
 }

}


To register the event listener, you need to add the following entries into your atlassian-plugin.xml


<component-import key="eventPublisher" interface="com.atlassian.event.api.EventPublisher" />
<component key="eventListener" class="at.a1ta.eap.tasktrack.jira.ppm.fieldsync.eventlistener.IssueListener">
 <description>Class that processes the incoming JIRA issue events.</description>
</component>

Tips and tricks


  • Do not forget to unregister the listener. If you do, the event handling goes several time at the background, without noticing it, an causes performance problems.
  • The event listener will be fired for all issues on the system. Therefore, in order to avoid performance drops, you need to decide as soon as possible, if the event is relevant to you. I suggest filtering the events by it's type.   


Nincsenek megjegyzések:

Megjegyzés küldése