【问题标题】:Handling events using ResourceChangeListener AEM 6.3使用 ResourceChangeListener AEM 6.3 处理事件
【发布时间】:2018-02-10 13:29:29
【问题描述】:

有人可以帮助我理解如何使用 osgi R6 注释实现 ResourceChangeListener 和处理事件吗? 我看到一个类似的帖子没有答案。AEM 6.3 - Creating Event handler using OSGi R6 annotations

【问题讨论】:

    标签: annotations osgi aem addeventlistener


    【解决方案1】:

    下面的代码 sn-p 注册了一个带有 OSGI R6 注释的 ResourceChangeListener。内联的 cmets 有解释。

    import java.util.List;
    import org.apache.sling.api.resource.observation.ResourceChange;
    import org.apache.sling.api.resource.observation.ResourceChangeListener;
    import org.osgi.service.component.annotations.Component;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @Component(
        service = ResourceChangeListener.class,
        property = {
            // filter the notifications by path in the repo. Can be array and supports globs
            ResourceChangeListener.PATHS+"="+"/content",
            //The type of change you want to listen to.
            //Possible values at https://sling.apache.org/apidocs/sling9/org/apache/sling/api/resource/observation/ResourceChange.ChangeType.html.
            ResourceChangeListener.CHANGES+"="+"ADDED",
            ResourceChangeListener.CHANGES+"="+"REMOVED",
            ResourceChangeListener.CHANGES+"="+"CHANGED"
    
            //PS: If you want to declare multiple values for a prop, you repeat it in OSGI R6 annotations.
            //https://stackoverflow.com/questions/41243873/osgi-r6-service-component-annotations-property-list#answer-41248826
        }
    )
    public class SampleResourceChangeListener implements ResourceChangeListener{ // Use ExternalResourceChangeListener to listen for changes that happen in a different node
      public static final Logger LOGGER = LoggerFactory.getLogger(SampleResourceChangeListener.class);
    
      //This method will be called with paths and types of change that occurred
      //The task in this should be fast. In case it takes more time, trigger a sling job from here.
      //The listener can be blacklisted if it's slow [it does for event handler, should be same for ResourceListener IMHO]
      @Override
      public void onChange(List<ResourceChange> list) {
        list.forEach((change) -> {
          LOGGER.info(change.getPath());
          LOGGER.info(change.getType().toString());
          //the methods to identify the property that changed are deprecated.
        });
      }
    } 
    

    Sling 中的一些参考实现

    1. https://github.com/apache/sling-org-apache-sling-discovery-commons/blob/4f7d7ca3224239d52798cc8418ec8283f5eddc9e/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java
    2. https://github.com/apache/sling-org-apache-sling-scripting-java/blob/89c28859a7df17a40eaaf2c26ee2433c98830204/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java

    【讨论】:

    • 非常感谢。现在在添加一个节点时,我的监听器被调用了。由于我的路径指向 DAM 资产路径,因此将为所有子节点以及 jcr:content、演绎版(每个单独)调用此侦听器。如何仅针对父节点而不是子节点限制它。 ?
    • 您只希望在节点类型 dam:Asset 或原始版本上触发它?
    • 是的..我正在尝试同样的事情..它正在工作..再次感谢您的回复。
    • 如果我需要单独配置 ResourceChangeListener.PATHS+"="+"/content",我需要在此处进行哪些更改?我创建了这个:@ObjectClassDefinition(name="test Listener") public @interface TsetConfiguration { @AttributeDefinition(name = "Resource Path", description = "Resource path where the file is present", type = AttributeType.STRING) String getResourcepath( ) 默认“/内容”; }但是如何将它用作 ResourcePath 属性?
    • 我猜你已经在activate方法中动态注册了资源变化监听器。查看 github.com/apache/sling-org-apache-sling-scripting-core/blob/… 中的 configureCache 方法。在使用新道具注册之前,请务必取消注册和现有的监听器
    猜你喜欢
    • 2018-04-06
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多