I have created in an even listener and this is the code. What I am looking to do in my Listener is to check if a particular property has been updated. If it is then take an action.
package com.aem.sites.interfaces;
import javax.jcr.observation.Event;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "Temperature Listener Configuration")
public @interface TemperatureListenerConfiguration {
@AttributeDefinition(
name = "propertyPath",
description = "Configurable paths for temperature event listener",
type = AttributeType.STRING
)
String getPath() default "/content/aemsite/en/jcr:content/root/responsivegrid/banner";
@AttributeDefinition(
name = "propetyName",
description = "Name of property",
type = AttributeType.STRING
)
String getPropertyName() default "temperature";
@AttributeDefinition(
name = "event types",
description = "Name of property",
type = AttributeType.INTEGER
)
int getEvent() default Event.PROPERTY_CHANGED;
}
package com.aem.sites.listeners;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aem.sites.interfaces.TemperatureListenerConfiguration;
@Component(immediate=true,
service= EventListener.class,
configurationPid = "com.aem.sites.listeners.TemperaturePropertyListener")
@Designate(ocd=TemperatureListenerConfiguration.class)
public class TemperaturePropertyListener implements EventListener{
/** The logger. */
private final Logger logger = LoggerFactory.getLogger(getClass());
/** The path. */
private String path;
/** The property name. */
private String propertyName;
/** The resolver factory. */
@Reference
private ResourceResolverFactory resolverFactory;
/** The resource resolver. */
private ResourceResolver resourceResolver;
/** The observation manager. */
private ObservationManager observationManager;
/** The session. */
private Session session;
/**
* Activate.
*
* @param config the config
*/
@Activate
@Modified
public void activate(TemperatureListenerConfiguration config) {
logger.info("**********************TemperatureListenerConfiguration:::activate********************** ");
path = config.getPath();
propertyName = config.getPropertyName();
int event = config.getEvent();
ObservationManager observationManager = getObservationManager();
registerListener(observationManager, event);
}
/**
* Gets the observation manager.
*
* @return the observation manager
*/
private ObservationManager getObservationManager() {
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "readService");
Resource resource = null;
try {
resourceResolver = resolverFactory.getServiceResourceResolver(param);
resource = resourceResolver.getResource(path);
session = resource.adaptTo(Session.class);
observationManager = session.getWorkspace().getObservationManager();
} catch (LoginException | RepositoryException e) {
logger.info("**********************Exception in get observation manager********************** "+e.getMessage());
}
return observationManager;
}
/**
* Register listener.
*
* @param observationManager the observation manager
* @param event the event
*/
private void registerListener(ObservationManager observationManager, int event) {
try {
observationManager.addEventListener(this, event, path, true, null, null, false);
logger.info("**********************TemperaturePropertyListener:::registerListener********************** ");
} catch (RepositoryException e) {
logger.info("**********************Exception in register add listener********************** "+e.getMessage());
}
}
/* (non-Javadoc)
* @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
*/
@Override
public void onEvent(EventIterator events) {
// TODO Auto-generated method stub
while(events.hasNext()) {
Event event = events.nextEvent();
logger.info("**********************TemperaturePropertyListener:::onEvent********************** ");
if(Event.PROPERTY_CHANGED == event.getType()) {
try {
Property changedProperty = session.getProperty(event.getPath());
if(changedProperty.getName().equalsIgnoreCase(propertyName)) {
logger.info("******************TemperaturePropertyListener::the name of the property changed is**************"+propertyName);
}
} catch (RepositoryException e) {
// TODO Auto-generated catch block
logger.info("**********************Repository exception in on event********************** "+e.getMessage());
}
}
}
}
/**
* Deactivate.
*
* @throws RepositoryException the repository exception
*/
@Deactivate
public void deactivate() throws RepositoryException {
if(observationManager != null) {
observationManager.removeEventListener(this);
}
if (session != null) {
session.logout();
session = null;
}
if(resourceResolver != null && resourceResolver.isLive()){
resourceResolver.close();
}
}
}
The value of the session in the method getObservationManager is null. The system user has all the privileges and is working fine elsewhere. Not sure what is wrong here.
I also tried to implement org.osgi.service.event.EventHandler following the aem-osgi-annotation-demo/SampleOsgiResourceListener.java at master · nateyolles/aem-osgi-annotation-... but turns out org.apache.sling.api.SlingConstants is now deprecated. So, I moved to using ResourceChangeListener and followed this acs-aem-samples/SampleResourceChangeListener.java at master · Adobe-Consulting-Services/acs-aem-samp... but found out that several methods in org.apache.sling.api.resource.observation.ResourceChange are now deprecated.
Thanks in advance
Solved! Go to Solution.
Views
Replies
Total Likes
I could get session now after whitelisting the bundle and using administrative session.
Views
Replies
Total Likes
For a test - try to put in this code and whitelist the bundle:
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
This will tell if issue is with the user or code in 6.3. If you are not sure about white list -see:
Views
Replies
Total Likes
I could get session now after whitelisting the bundle and using administrative session.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies