简体   繁体   English

AEM 6.3-使用OSGi R6批注创建事件处理程序

[英]AEM 6.3 - Creating Event handler using OSGi R6 annotations

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. 我通过遵循https://github.com/nateyolles/aem-osgi-annotation-demo/blob/master/core/src/main/java/com/nateyolles/aem/osgiannotationdemo/core/listeners/创建了事件处理程序SampleOsgiResourceListener.java ,它工作正常。 However, I get the warning "The field SlingConstants.TOPIC_RESOURCE_ADDED is deprecated". 但是,我收到警告“字段SlingConstants.TOPIC_RESOURCE_ADDED已过时”。 I did some searching and found this thread : https://forums.adobe.com/thread/2325819 我进行了一些搜索,发现了这个线程: https : //forums.adobe.com/thread/2325819

Here are the challenges that I am facing: 这是我面临的挑战:

1) I want to create a separate configuration interface for my event handler. 1)我想为我的事件处理程序创建一个单独的配置接口。 I tried this and it isn't working 我尝试了这个,但是没有用

package 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. 我也想要SlingConstants不推荐使用的解决方案。 Not sure if ResourceChangeListener is the answer to my problem and if yes then how everything is going to work together in the code. 不知道ResourceChangeListener是否是我的问题的答案,如果是,那么一切如何在代码中一起工作。

Thanks in advance 提前致谢

=============================== Latest Code ===============================最新代码

package 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 Interface 介面

package 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.html 在此处查看sling 9文档中的org.apache.sling.api.SlingConstants的Javadoc: http : //sling.apache.org/apidocs/sling9/org/apache/sling/api/SlingConstants.html

it tells you specifically that TOPIC_RESOURCE_ADDED is deprecated: 它明确告诉您不赞成使用TOPIC_RESOURCE_ADDED

Deprecated. 已过时。 Register a ResourceChangeListener instead 而是注册一个ResourceChangeListener

Read the documentation for ResourceChangeListener , additionally, you can take a look at a sample SCR service impl from ACS Samples: 阅读ResourceChangeListener的文档,此外,您还可以查看ACS Samples的示例SCR服务提示

It should not be hard to convert that to R6 declarative service. 将其转换为R6声明式服务应该不难。

Also, here are two examples from the sling project ResourceBackedPojoChangeMonitor and OsgiObservationBridge 另外,这是悬吊项目ResourceBackedPojoChangeMonitorOsgiObservationBridge的两个示例

Try to mimic those classes with the properties in the same class. 尝试使用相同类中的属性来模仿这些类。

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

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