javaaemapache-felixsling

How to create JobConsumer in Sling?


According to Apache Sling official site(https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html#job-consumers), this is the way to write 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;
    }
}

However @Service and @Property are both deprecated annotations. I want to know the proper way to create JobConsumer. Does anyone know how to write a code equivalent to the above?


Solution

  • The scr annotations are deprecated in AEM and it is recommended to use the official OSGi Declarative Services annotations going forward. There is a seminar by Adobe on using the OSGi R7 annotations.

    The new way of writing the same would be

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