Last updated Mar 5, 2025

Creating an object event listener for Assets

Level of experience:

Intermediate. You should have completed at least one beginner tutorial before working through this tutorial. See the list of developer tutorials.

Time estimate:

It should take you approximately one hour to complete this tutorial.

Applicable:

Jira Service Management 5.12 and later.

This tutorial shows you how to create your own Assets event listener, listening on object events. This tutorial assumes that you’ve already set up your app. Set up your Assets app

Plugin descriptor configuration

To begin, install Assets locally in your Maven repository. Explore how to install third part JARs

Next, specify the dependency in the app configuration or as an annotation. In atlassian-plugin.xml, this should look like this:

1
2
<component-import key="objectFacade"
    interface="com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"/>

Listen to Object Create event

This Java example shows how to listen to the Object Create event and log the affected object.

1
2
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);
    }
}

Listen to Object update event

This Java example shows how to listen to the Object Update event and get detailed information about affected attributes.

1
2
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);
    }
}

Congratulations!

You've built an object event listener in Jira Service Management Data Center Assets.

Next steps

You might want to explore other tutorials for Assets app developments:

Rate this page: