如何在 Sling 中创建 JobConsumer?

问题描述

根据 Apache Sling 官方网站(https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html#job-consumers),这是编写 JobConsumer 的方式。

import org.apache.Felix.scr.annotations.Component;
import org.apache.Felix.scr.annotations.Service;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;

@Component
@Service(value={JobConsumer.class})
@Property(name=JobConsumer.PROPERTY_TOPICS,value="my/special/jobtopic",)
public class MyJobConsumer implements JobConsumer {

    public JobResult process(final Job job) {
        // process the job and return the result
        return JobResult.OK;
    }
}

然而@Service 和@Property 都是不推荐使用的注解。 我想知道创建 JobConsumer 的正确方法。 有谁知道如何编写与上述等效的代码

解决方法

scr 注释在 AEM 中已弃用,建议继续使用官方 OSGi 声明式服务注释。有一个关于使用 OSGi R7 注释的 seminar by Adobe

新的写法是

import org.osgi.service.component.annotations.Component;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;

@Component(
    immediate = true,service = JobConsumer.class,property = {
        JobConsumer.PROPERTY_TOPICS +"=my/special/jobtopic"
    }
)
public class MyJobConsumer implements JobConsumer {

    public JobResult process(final Job job) {
        // process the job and return the result
        return JobResult.OK;
    }
}