如何在AEM中使用EventListener处理节点移动事件?重复通话

时间:2019-01-02 12:26:34

标签: events osgi aem cq5 jcr

我有实现javax.jcr.observation.EventListener的服务。该服务正在侦听以下事件类型:

  • Event.NODE_MOVED
  • Event.NODE_REMOVED

当我移动节点myCustomCodeToExecute()时,方法被触发两次。

有没有办法只有在有人移动节点时才触发我的代码一次?

    executor.submit(() -> {
        List<Event> eventsList = IteratorUtils.toList(eventIterator);
        for (final Event event : eventsList) {
            try {
                myCustomCodeToExecute()
            } catch (Exception e) {
                LOGGER.error("Can't send event", e);
            }
        }
    });

REMOVE事件按预期运行

1 个答案:

答案 0 :(得分:1)

我找到了解决方法:

@Override
public void onEvent(EventIterator eventIterator) {
    executor.submit(() -> {
        List<Event> eventsList = IteratorUtils.toList(eventIterator);
        for (final Event event : eventsList) {
            try {
                if (eventType == Event.NODE_REMOVED) {
                    if (eventsList.stream().noneMatch(e -> Event.NODE_MOVED == e.getType())) {
                         //remove event
                    }
                         //move event will be processed in next iteration
                         continue;
                    }
                }
            } catch (Exception e) {
                LOGGER.error("Can't send event", e);
            }
        }
    });
}