TemperatureListenerConfiguration

TemperatureListenerConfiguration

它具体告诉您TOPIC_RESOURCE_ADDED已过时: 已弃用.而是注册 ResourceChangeListener阅读ResourceChangeListener的文档,此外,您还可以查看来自ACS Samples的示例SCR服务展示:将其转换为R6声明式服务应该不难.此外,这是悬带项目的两个示例 ResourceBackedPojoChangeMonitor 和 OsgiObservationBridge尝试使用相同类中的属性来模仿这些类.I have created an Event handler by following https://github.com/nateyolles/aem-osgi-annotation-demo/blob/master/core/src/main/java/com/nateyolles/aem/osgiannotationdemo/core/listeners/SampleOsgiResourceListener.java and it works fine. However, I get the warning "The field SlingConstants.TOPIC_RESOURCE_ADDED is deprecated". I did some searching and found this thread :https://forums.adobe.com/thread/2325819Here are the challenges that I am facing:1) I want to create a separate configuration interface for my event handler. I tried this and it isn't workingpackage com.aem.sites.interfaces;import org.apache.sling.api.SlingConstants;import org.osgi.service.event.EventConstants;import org.osgi.service.metatype.annotations.AttributeDefinition;import org.osgi.service.metatype.annotations.AttributeType;import org.osgi.service.metatype.annotations.ObjectClassDefinition;@ObjectClassDefinition(name = "Temperature Listener Configuration")public @interface TemperatureListenerConfiguration { @AttributeDefinition( name = EventConstants.EVENT_FILTER, description = "Configurable paths for temperature event listener", type = AttributeType.STRING ) String getPaths() default "/content/aemsite/en/jcr:content/root/responsivegrid/banner"; @AttributeDefinition( name = EventConstants.EVENT_TOPIC, description = "Event types", type = AttributeType.STRING ) String[] getEventTypes() default {SlingConstants.TOPIC_RESOURCE_ADDED,SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED};}package com.aem.sites.listeners;import org.osgi.service.component.annotations.Activate;import org.osgi.service.component.annotations.Component;import org.osgi.service.component.annotations.Modified;import org.osgi.service.event.Event;import org.osgi.service.event.EventHandler;import org.osgi.service.metatype.annotations.Designate;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.aem.sites.interfaces.TemperatureListenerConfiguration;@Component(immediate=true,service=EventHandler.class,configurationPid = "com.aem.sites.listeners.EventHandler")@Designate(ocd=TemperatureListenerConfiguration.class)public class TemperaturePropertyListener implements EventHandler{ private final Logger logger = LoggerFactory.getLogger(getClass()); @Override public void handleEvent(Event event) { logger.info("*********************Event handler*****************************"); } @Activate @Modified public void activate(TemperatureListenerConfiguration config) { //config.getPaths(); logger.info("**************************TemperaturePropertyListener******************activate**********************"); }}I also want the solution for SlingConstants deprecated issue. Not sure if ResourceChangeListener is the answer to my problem and if yes then how everything is going to work together in the code.Thanks in advance===============================Latest Codepackage com.aem.sites.listeners;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.Activate;import org.osgi.service.component.annotations.Component;import org.osgi.service.component.annotations.Modified;import org.osgi.service.metatype.annotations.Designate;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.aem.sites.interfaces.TemperatureListenerConfiguration;@Component(immediate=true,service=ResourceChangeListener.class,configurationPid = "com.aem.sites.listeners.TemperaturePropertyListener")@Designate(ocd=TemperatureListenerConfiguration.class)public class TemperaturePropertyListener implements ResourceChangeListener{ private final Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onChange(List<ResourceChange> changes) { for (final ResourceChange change : changes) { logger.info("**************************TemperaturePropertyListener******************change type**********************"+change.getType()); } } @Activate @Modified public void activate(TemperatureListenerConfiguration config) { //config.getPaths(); logger.info("**************************TemperaturePropertyListener******************activate**********************"); }}The Interfacepackage com.aem.sites.interfaces;import org.apache.sling.api.resource.observation.ResourceChangeListener;import org.osgi.service.metatype.annotations.AttributeDefinition;import org.osgi.service.metatype.annotations.AttributeType;import org.osgi.service.metatype.annotations.ObjectClassDefinition;@ObjectClassDefinition(name = "Temperature Listener Configuration")public @interface TemperatureListenerConfiguration { @AttributeDefinition( name = ResourceChangeListener.PATHS, description = "Configurable paths for temperature event listener", type = AttributeType.STRING ) String[] getPaths() default {"/content/aemsite/en/jcr:content/root/responsivegrid/banner"}; @AttributeDefinition( name = ResourceChangeListener.CHANGES, description = "Event types", type = AttributeType.STRING ) String[] getEventTypes() default {"ADDED","REMOVED","CHANGED","PROVIDER_ADDED", "PROVIDER_REMOVED"};} 解决方案 Looking at the Javadoc for org.apache.sling.api.SlingConstants in sling 9 documentation here: http://sling.apache.org/apidocs/sling9/org/apache/sling/api/SlingConstants.htmlit tells you specifically that TOPIC_RESOURCE_ADDED is deprecated: Deprecated. Register a ResourceChangeListener insteadRead the documentation for ResourceChangeListener, additionally, you can take a look at a sample SCR service impl from ACS Samples:It should not be hard to convert that to R6 declarative service.Also, here are two examples from the sling project ResourceBackedPojoChangeMonitor and OsgiObservationBridgeTry to mimic those classes with the properties in the same class. 这篇关于AEM 6.3-使用OSGi R6批注创建事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 03:54