Hello,
I'm stuck at following situation:
I have a page and I want add some events to it. The content structure is
/content
/site
/mypage
/jcr:content
The node 'mypage' has the primary type 'cq:Page' and node 'jcr:content' the type 'cq:PageContent'.
To add events I created a service:
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.*;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import java.util.*;
@Component(
service = MyServiceListener.class,
immediate= true)
public class MyServiceListener implements EventListener {
private SlingSettingsService settings;
private ResourceResolverFactory resourceResolverFactory;
private SlingRepository repository;
private ResourceResolver resolver;
private Session session = null;
protected void activate() {
try {
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "aserviceuser");
ResourceResolver resolver = null;
resolver = resourceResolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);
session.getWorkspace().getObservationManager().addEventListener(new TestOnEvent(),
Event.PROPERTY_CHANGED | Event.PROPERTY_ADDED | Event.PROPERTY_REMOVED, "/content/site/mypage/jcr:content", false, null, null, false);
} catch (Exception e) {
;
}
}
protected void deactivate() throws RepositoryException {
if (session != null) {
session.logout();
session = null;
}
}
}
Whenever the bundle is (re)started the above code works fine. So I added code for actions for defined events:
public void onEvent(EventIterator events)
{
try {
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "aserviceuser");
ResourceResolver resourceResolver = null;
resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);
session = resourceResolver.adaptTo(Session.class);
while (events.hasNext()) {
...
}
} catch (Exception e) {
;
}
}
My Problem is, this piece of code won't always be executed. I could see his behaviour by adding output to logfiles and break points for the debug mode.
When some bundles are renewed then existing a good chance that above code will be executed. After that he won't be executed anymore.
My question: Where is my mistake?
Thanks in advanced
PS: The service user got all nessecary rights to read the content path.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
Please check the example of JCR, Sling and Resource listener
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleJCREvent.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TestEventHandler.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleResourceChangeListener.java
Can you try if this works in your local?
Yes.
Hi,
Please check the example of JCR, Sling and Resource listener
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleJCREvent.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TestEventHandler.java
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleResourceChangeListener.java
Can you try if this works in your local?
Today, I read your provided links. Due to lack of time I checked and tried first one. This example is same as mine. I added in method "deactivated()" the code for removing eventlistener. Though the behavior is still the same.
Via debug mode I found if the target page recently created, the event will be fired. After that no other events will be fired, no matter wich action I did.
By the wy: When I debuged into
session.getWorkspace().getObservationManager().getRegisteredEventListeners()
I found follow message "java.lang.Exception: Event listener is already here:" I checked my instance and my code, there are no multiple assignments of event handler.
Hi,
Are you changing the page properties at
/content/site/mypage/jcr:content
Please check this https://developer.adobe.com/experience-manager/reference-materials/spec/jsr170/javadocs/jcr-1.0/java...
Views
Likes
Replies