2017-07-17 52 views
1

我正在尝试使用观察管理器实现一个Event监听器。我需要听取多条路径。aem cq多个路径上的监听器

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

我已经试过这样的事情

String pathvalues = "path1,path2,path3"; 
List <String> path = Arrays.asList(pathvalues.split(",")); 
session = repository.loginAdministrative(repository.getDefaultWorkspace()); 
observationManager = session.getWorkspace().getObservationManager(); 
for (String paths: path) { 
observationManager.addEventListener(this, Event.NODE_ADDED, paths, true, null, null, false); 
} 

不过这听上只已经重复的最后一条路径。我想这是有道理的,所以我仍然坚持。

,我发现这个在网络上, SLING-4564

我实现它,但不知其仍然不听。

如果有人有任何意见,请让我知道。

回答

2

的EventConstants.EVENT_FILTER财产关于第一个问题:

observationManager.addEventListener(this, Event.NODE_ADDED, paths, true, null, null, false);

不过这听上只有最后的路径已经重复了上

这是设计。偶数听众只注册到一条路径,并且循环中提供的最后一条路径将成为活动路径。最后一次拨打addEventListener之前的其他内容将被忽略。

有您的问题2个可能的解决方案:

注册您的父级监听器和筛选上,甚至调用的事件。

OR

使用eventFilter.setAdditionalPaths(paths);为在SLING-4564

首先描述的方法过滤的多条路径的事件是更加希望的,因为它可以让你根据你的逻辑,实现最优和快速过滤。例如,您的快速过滤器可能会排除/ etc /,因此您可以轻松评估事件路径并检查它是以/ etc /开头还是放弃它。

SLING-4564基本上是为OAK启动等大型活动而设计的,并且作为性能的一般指导原则,您的应用程序特定事件侦听器应限于应用程序或内容特定路径,而不应局限于全局性。

+0

嗯,感谢您的信息。将采用第一种在根路径上侦听的方法,但如果我想听不同的根路径,它仍然有点麻烦。 – calculus

2

检查在下面的例子中

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); 

     } 
    } 
} 
+0

还没有试过这个。将放弃它,谢谢! – calculus