多路径上的aem cq侦听器

多路径上的aem cq侦听器,aem,event-listener,sling,Aem,Event Listener,Sling,我正在尝试使用观察管理器实现一个事件侦听器。我需要在多个路径上进行监听 但是,我相信,我们只能在一条路径上注册一个侦听器。有没有一种方法可以在多条路径上进行监听 我试过这样的东西 String pathvalues=“路径1、路径2、路径3”; Listpath=Arrays.asList(pathvalues.split(“,”); session=repository.loginAdministrative(repository.getDefaultWorkspace()); observa

我正在尝试使用观察管理器实现一个事件侦听器。我需要在多个路径上进行监听

但是,我相信,我们只能在一条路径上注册一个侦听器。有没有一种方法可以在多条路径上进行监听

我试过这样的东西

String pathvalues=“路径1、路径2、路径3”;
Listpath=Arrays.asList(pathvalues.split(“,”);
session=repository.loginAdministrative(repository.getDefaultWorkspace());
observationManager=session.getWorkspace().getObservationManager();
用于(字符串路径:路径){
observationManager.addEventListener(这个,Event.NODE_添加,路径,true,null,null,false);
}
但这只是在它迭代的最后一条路径上侦听。我想这是有道理的,所以我还是被卡住了

我在网上找到了这个,

我实现了它,但不知怎么的,它仍然没有听


如果有人有任何输入,请务必告诉我。

检查以下示例中的EventConstants.EVENT\u筛选器属性

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.Service;
import org.apache.sling.api.SlingConstants;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

@Component
/**
 * The @Component annotation is the only required annotation. If this annotation is not declared for a Java class, the class is
 * not declared as a component.
 */
@Service(value = EventHandler.class)
/**
 * The @Service annotation defines whether and which service interfaces are provided by the component. This is a class annotation.
 * This annotation is used to declare <service> and <provide> elements of the component declaration.
 */
@Properties({
        @Property(name = EventConstants.EVENT_TOPIC,
                value = { SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED }),
        @Property(name = EventConstants.EVENT_FILTER, value = "(|(path=/content/dam/*/jcr:content)(assetPath=/content/dam/*))") })
public class ExampleServiceImpl implements EventHandler {

    final String[] eventProps = { "resourceAddedAttributes", "resourceChangedAttributes", "resourceRemovedAttributes" };

    public void handleEvent(org.osgi.service.event.Event event) {
        for (String eventProp : eventProps) {
            String[] props = (String[]) event.getProperty(eventProp);

        }
    }
}
import org.apache.felix.scr.annotations.Component;
导入org.apache.felix.scr.annotations.Properties;
导入org.apache.felix.scr.annotations.Property;
导入org.apache.felix.scr.annotations.Service;
导入org.apache.sling.api.SlingConstants;
导入org.osgi.service.event.EventConstants;
导入org.osgi.service.event.EventHandler;
@组成部分
/**
*@Component注释是唯一需要的注释。如果未为Java类声明此注释,则该类为
*未声明为组件。
*/
@服务(值=EventHandler.class)
/**
*@Service注释定义组件是否提供服务接口以及提供哪些服务接口。这是一个类注释。
*此注释用于声明和组件声明的元素。
*/
@性质({
@属性(名称=EventConstants.EVENT_主题,
值={SlingConstants.TOPIC_RESOURCE_ADDED,SlingConstants.TOPIC_RESOURCE_CHANGED,SlingConstants.TOPIC_RESOURCE_REMOVED}),
@属性(name=EventConstants.EVENT_FILTER,value=“(|(path=/content/dam/*/jcr:content)(assetPath=/content/dam/*))”))
公共类ExampleServiceImpl实现EventHandler{
最后一个字符串[]eventProps={“resourceAddedAttributes”、“resourceChangedAttributes”、“resourceRemovedAttributes”};
public void handleEvent(org.osgi.service.event.event){
for(字符串eventProp:eventProps){
String[]props=(String[])event.getProperty(eventProp);
}
}
}

检查以下示例中的EventConstants.EVENT\u筛选器属性

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.Service;
import org.apache.sling.api.SlingConstants;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

@Component
/**
 * The @Component annotation is the only required annotation. If this annotation is not declared for a Java class, the class is
 * not declared as a component.
 */
@Service(value = EventHandler.class)
/**
 * The @Service annotation defines whether and which service interfaces are provided by the component. This is a class annotation.
 * This annotation is used to declare <service> and <provide> elements of the component declaration.
 */
@Properties({
        @Property(name = EventConstants.EVENT_TOPIC,
                value = { SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED }),
        @Property(name = EventConstants.EVENT_FILTER, value = "(|(path=/content/dam/*/jcr:content)(assetPath=/content/dam/*))") })
public class ExampleServiceImpl implements EventHandler {

    final String[] eventProps = { "resourceAddedAttributes", "resourceChangedAttributes", "resourceRemovedAttributes" };

    public void handleEvent(org.osgi.service.event.Event event) {
        for (String eventProp : eventProps) {
            String[] props = (String[]) event.getProperty(eventProp);

        }
    }
}
import org.apache.felix.scr.annotations.Component;
导入org.apache.felix.scr.annotations.Properties;
导入org.apache.felix.scr.annotations.Property;
导入org.apache.felix.scr.annotations.Service;
导入org.apache.sling.api.SlingConstants;
导入org.osgi.service.event.EventConstants;
导入org.osgi.service.event.EventHandler;
@组成部分
/**
*@Component注释是唯一需要的注释。如果未为Java类声明此注释,则该类为
*未声明为组件。
*/
@服务(值=EventHandler.class)
/**
*@Service注释定义组件是否提供服务接口以及提供哪些服务接口。这是一个类注释。
*此注释用于声明和组件声明的元素。
*/
@性质({
@属性(名称=EventConstants.EVENT_主题,
值={SlingConstants.TOPIC_RESOURCE_ADDED,SlingConstants.TOPIC_RESOURCE_CHANGED,SlingConstants.TOPIC_RESOURCE_REMOVED}),
@属性(name=EventConstants.EVENT_FILTER,value=“(|(path=/content/dam/*/jcr:content)(assetPath=/content/dam/*))”))
公共类ExampleServiceImpl实现EventHandler{
最后一个字符串[]eventProps={“resourceAddedAttributes”、“resourceChangedAttributes”、“resourceRemovedAttributes”};
public void handleEvent(org.osgi.service.event.event){
for(字符串eventProp:eventProps){
String[]props=(String[])event.getProperty(eventProp);
}
}
}
对于您的第一期:

observationManager.addEventListener(这个,Event.NODE_添加,路径,true,null,null,false)

但这只是在它迭代的最后一条路径上侦听

这是设计的。偶数侦听器只注册到一个路径,循环中提供的最后一个路径将是活动路径。最后一次调用
addEventListener
之前的所有其他内容都将被忽略

您的问题有两种可能的解决方案:

在父级注册侦听器,并在偶数调用时过滤事件

使用
eventFilter.setAdditionalPath(路径)
过滤多个路径上的事件,如中所述

第一种方法更可取,因为它允许您根据逻辑实现最佳和快速过滤。例如,您的快速过滤器可以是排除/etc/,这样您就可以轻松地评估事件路径,检查它是否以/etc/开头,并放弃它

基本上是为大型活动(如OAK startup)而设计的,作为性能的一般准则,特定于应用程序的事件侦听器应限于特定于应用程序或内容的路径,而不是全局性的。关于第一期:

observationManager.addEventListener(这个,Event.NODE_添加,路径,true,null,null,false)

但这只是在它迭代的最后一条路径上侦听

这是设计的。偶数侦听器只注册到一个路径,循环中提供的最后一个路径将是活动路径。最后一次调用
addEventListener
之前的所有其他内容都将被忽略

您的问题有两种可能的解决方案:

在父级注册侦听器,并在偶数调用时过滤事件

使用
ev