Search code examples
eventsosgiaemjcr

How to process node move event with EventListener in AEM? Duplicate calls occurs


I have service that implements javax.jcr.observation.EventListener. This service is listening the following event types:

  • Event.NODE_MOVED
  • Event.NODE_REMOVED

When I move node myCustomCodeToExecute() method is triggered twice.

Is there a way to trigger my code only once if someone moves node?

    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 event works as expected


Solution

  • I found an solution:

    @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);
                }
            }
        });
    }