使用ResourceChangeListener AEM 6.3处理事件

时间:2018-02-10 13:29:30

标签: annotations osgi aem addeventlistener

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

1 个答案:

答案 0 :(得分:4)

下面的代码片段使用OSGI R6注释注册ResourceChangeListener。内联评论有解释。

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