Create an object event listener

On this page

Still need help?

The Atlassian Community is here for you.

Ask the community

On this page you will learn how to create you own Assets event listener, listening on object events. We assume that you have setup your app as described here.

Plugin descriptor configuration

You also need to specify the dependency in the plugin configuration (or as annotation).

In atlassian-plugin.xml if will look like this:

<component key="objectEventListener" class="com.example.event.listener.ObjectEventListenerImpl">
    <decription>Object Event Listener</decription>
</component>

Examples

Object create

Java Example

This is an example how to listening on "Object Create" event and log out the affected object.

package com.example.event.listener;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
 
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.user.ApplicationUser;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectAsyncEvent;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectEventListener;
import com.riadalabs.jira.plugins.insight.services.events.objects.event.InsightObjectCreatedEvent;
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean;
 
public class ObjectEventListenerImpl implements InitializingBean, DisposableBean, InsightObjectEventListener {
 
    private static final Logger logger = LoggerFactory.getLogger(ObjectEventListenerImpl.class);
    private final EventPublisher eventPublisher;
 
    /**
     * Constructor.
     *
     * @param eventPublisher
     */
    public ObjectEventListenerImpl(final EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
 
    @EventListener
    public void onAsyncEvent(InsightObjectAsyncEvent insightObjectEvent) {
        switch (insightObjectEvent.getEventType()) {
        case OBJECT_CREATED:
            InsightObjectCreatedEvent insightObjectCreatedEvent = (InsightObjectCreatedEvent) insightObjectEvent;
            ObjectBean objectBean = insightObjectCreatedEvent.getObjectBean();
            ApplicationUser user = insightObjectCreatedEvent.getRunAsUser();
            logger.warn("User: " + user + " created object: " + objectBean);
            break;
        default:
            break;
        }
    }
 
    /**
     * Called when the plugin has been enabled.
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
    }
 
    /**
     * Called when the plugin is being disabled or removed.
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }
 
}


Object update

Java Example

This is an example how to listening on "Object Update" event and get detailed information about affected attributes.

package com.example.event.listener;
  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
  
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.user.ApplicationUser;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectAsyncEvent;
import com.riadalabs.jira.plugins.insight.services.events.objects.InsightObjectEventListener;
import com.riadalabs.jira.plugins.insight.services.events.objects.event.InsightObjectCreatedEvent;
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean;
  
public class ObjectEventListenerImpl implements InitializingBean, DisposableBean, InsightObjectEventListener {
  
    private static final Logger logger = LoggerFactory.getLogger(ObjectEventListenerImpl.class);
    private final EventPublisher eventPublisher;
  
    /**
     * Constructor.
     *
     * @param eventPublisher
     */
    public ObjectEventListenerImpl(final EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
  
    @EventListener
    public void onAsyncEvent(InsightObjectAsyncEvent insightObjectEvent) {
        switch (insightObjectEvent.getEventType()) {
        case OBJECT_UPDATED:
            InsightObjectUpdatedEvent insightObjectUpdatedEvent = (InsightObjectUpdatedEvent) insightObjectEvent;
            ObjectBean objectBean = insightObjectUpdatedEvent.getObjectBean();
            ApplicationUser user = insightObjectCreatedEvent.getRunAsUser();
            logger.warn("User: " + user + " updated object: " + objectBean);
 
            List<NotificationUpdateBean> updatedAttributes = insightObjectUpdatedEvent.getNotificationUpdateBeans();
            for (NotificationUpdateBean updatedAttribute : updatedAttributes) {
                String attributeName = updatedAttribute.getAttributeName();
                List<String> addedValues = updatedAttribute.getAddedValues();
                List<String> removedValues = updatedAttribute.getRemovedValues();
                logger.warn("Updated attribute " + attributeName + ", added attribute value(s): " + addedValues + ", removed attribute value(s): " + removedValues);
            }
            break;
        default:
            break;
        }
    }
  
    /**
     * Called when the plugin has been enabled.
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
    }
  
    /**
     * Called when the plugin is being disabled or removed.
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }
  
}



Last modified on Sep 26, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.