Hi @ryuushihou1990,
When AEM does a copy-paste via the Sites toolbar:
-
It first creates the node, triggering your listener.
-
The child nodes (like jcr:content) might not be fully available yet.
-
Your code immediately attempts to access the full structure, leading to errors.
1. Use a Delayed Job Instead of Immediate Action
Instead of modifying the node directly in the ResourceChangeListener, enqueue a delayed Job (Sling Job or Scheduler) that acts on the node after a few seconds, giving time for the copy to complete.
Example:
jobManager.addJob("remove/onTime",
Collections.singletonMap("path", path));
Then create a Job Consumer to handle the property removal after a short delay.
2. Wrap Node Access in Retry Logic
If you prefer to stay within the listener, implement a retry with exponential backoff:
int retries = 3;
while (retries-- > 0) {
if (session.nodeExists(path)) {
Node node = session.getNode(path);
if (node.hasProperty("onTime")) {
node.getProperty("onTime").remove();
session.save();
}
break;
}
Thread.sleep(500);
}
This is not the most elegant approach but may work for limited cases. Use with caution.
3. Target jcr:content Directly
In some scenarios, the copy-paste triggers on the top-level node (e.g., /content/site/en/page), but the onTime is inside jcr:content.
So if your goal is to remove the onTime from jcr:content, modify:
Node node = session.getNode(path + "/jcr:content");
But again, only after ensuring the node exists (via retry or delay).
Hope that helps!