Introduction
You can develop a custom event handler for Adobe Experience Manager (AEM)
Listerner Class:
package com.geometrixx.sightly.Listeners;
import java.util.Arrays;
import javax.jcr.Node;
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.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.tagging.JcrTagManagerFactory;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
@Component
public class TagListener implements EventListener {
private final Logger LOGGER = LoggerFactory
.getLogger(TagListener.class);
private BundleContext bundleContext;
@Reference
private ResourceResolverFactory resolverFactory;
private static final String NAMESPACE = "/etc/tags/mysample";
@Reference
JcrTagManagerFactory tmf;
private Session session;
private ObservationManager observationManager;
public void run() {
LOGGER.info("Running...");
}
protected void activate(ComponentContext ctx) {
this.bundleContext = ctx.getBundleContext();
try
{
//Invoke the adaptTo method to create a Session
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
// Setup the event handler to respond to a new claim under content/claim....
observationManager = session.getWorkspace().getObservationManager();
final String[] types = { "cq:Page","nt:unstructured"};
final String path = "/content"; // define the path
observationManager.addEventListener(this, Event.NODE_ADDED, path, true,
null, null, true);
LOGGER.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path);
}
catch(Exception e)
{
LOGGER.error("unable to register session",e);
}
}
protected void deactivate(ComponentContext componentContext) throws RepositoryException {
if(observationManager != null) {
observationManager.removeEventListener(this);
}
if (session != null) {
session.logout();
session = null;
}
}
@Override
public void onEvent(EventIterator it) {
// TODO Auto-generated method stub
LOGGER.info("IN ONEVENT!");
try {
while (it.hasNext()) {
//Custom Logic Goes Here.
}
}catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
Above class is sample Event Listener Class customize the business logic in OnEvent method which is overridden
To get more info on Events properties please refer the documentation
https://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/observation/Event.html
To get adobe article on Event Listener refer the article
https://helpx.adobe.com/experience-manager/using/tagmanager.html
Article also describes more information about TagManager Class and to use it.
No comments:
Post a Comment