简体   繁体   English

使用ResourceChangeListener AEM 6.3处理事件

[英]Handling events using ResourceChangeListener AEM 6.3

Can someone please help me in understanding how to implement a ResourceChangeListener and handle events using osgi R6 annotations? 有人可以帮助我了解如何实现osgi R6注释的ResourceChangeListener和处理事件吗? I saw a similar post with no answer. 我看到类似的帖子,没有答案。 AEM 6.3 - Creating Event handler using OSGi R6 annotations AEM 6.3-使用OSGi R6批注创建事件处理程序

The code snippet below registers a ResourceChangeListener with OSGI R6 annotations. 下面的代码段使用OSGI R6注释注册了ResourceChangeListener。 The comments inline have the explanation. 内联注释中有解释。

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

Some reference implementations within Sling 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 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 https://github.com/apache/sling-org-apache-sling-scripting-java/blob/89c28859a7df17a40eaaf2c26ee2433c98830204/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM