オブジェクト イベント リスナーの作成
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.
プラグイン記述子の設定
また、プラグインの設定で (アノテーションとして) 依存関係を指定する必要があります。
atlassian-plugin.xml では、次のようになります。
<component key="objectEventListener" class="com.example.event.listener.ObjectEventListenerImpl">
<decription>Object Event Listener</decription>
</component>
例
オブジェクト作成
Java の例
この例は、"オブジェクト作成" イベントをリッスンし、影響を受けるオブジェクトをログアウトする方法を示しています。
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);
}
}
オブジェクト更新
Java の例
この例は、"オブジェクト更新" イベントをリッスンし、影響を受ける属性に関する詳細情報を取得する方法を示しています。
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);
}
}
最終更新日 2022 年 9 月 26 日
Powered by Confluence and Scroll Viewport.