Smr*_*thi 2 osgi annotations addeventlistener aem
有人可以帮助我理解如何实现 ResourceChangeListener 并使用 osgi R6 注释处理事件吗?我看到一个类似的帖子,没有答案。AEM 6.3 - 使用 OSGi R6 注释创建事件处理程序
下面的代码片段使用 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.
///sf/ask/2887071141/#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.
});
}
}
Run Code Online (Sandbox Code Playgroud)
Sling 中的一些参考实现
| 归档时间: |
|
| 查看次数: |
6234 次 |
| 最近记录: |