我想获取CQ 561中特定节点的更改列表。 此外,我还需要更改列表中的先前值和修改后的值。
答案 0 :(得分:2)
AFAIK没有编码就无法获得该功能 您可以使用 javax.jcr.observation.ObservationManager 来观察JCR节点及其属性 创建一个JCR EventListener 并在ObservationManager中注册它,可以从JCR会话中重新获取。有关详细信息,请参阅example 但 javax.jcr.observation.Event 不包含以前的值,因此您必须自己组织此逻辑。
package com.adobe.aem.events;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import javax.jcr.Node;
import javax.jcr.Repository;
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.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolverFactory ;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* Just a simple DS Component
*/
@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable,EventListener {
private Logger log = LoggerFactory.getLogger(this.getClass());
private BundleContext bundleContext;
//Inject a Sling ResourceResolverFactory
@Reference
private ResourceResolverFactory resolverFactory;
private Session session;
private ObservationManager observationManager;
//Inject a Sling ResourceResolverFactory to create a Session requited by the EventHandler
@Reference
private SlingRepository repository;
public void run() {
log.info("Running...");
}
//Place app logic here to define the AEM Custom Event Handler
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 = { "nt:unstructured","sling:Folder" };
final String path = "/content/claim"; // define the path
observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, types, false);
log.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path);
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void deactivate(ComponentContext componentContext) throws RepositoryException {
if(observationManager != null) {
observationManager.removeEventListener(this);
}
if (session != null) {
session.logout();
session = null;
}
}
//Define app logic that is fired when the event occurs - simply track the time
//when the event occurred.
public void onEvent(EventIterator itr) {
Calendar cal = Calendar.getInstance();
//log the time when the event occurred
log.info("A new node was added to content/claim at : " +cal.getTime());
}
}
另见Javadoc:
可以通过ObservationManager对象注册EventListener。异步通知事件侦听器,并在事件发生并提交事务后查看事件。事件侦听器仅查看为其注册的会话具有足够访问权限的事件。