EventListner vs Event handler any difference?
Osgi event handling vs sling event handling, both handle resource path any other major difference?
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @user96222
In the context of Adobe Experience Manager (AEM), let's clarify the distinctions between EventListener, EventHandler, OSGi event handling, and Sling event handling:
EventListener vs. EventHandler:
OSGi Event Handling vs. Sling Event Handling:
Key Differences:
In summary, while there may be some overlapping terminology, it's essential to consider the context in which these terms are used within AEM. EventListeners and EventHandlers may be used interchangeably depending on the use case, while OSGi and Sling event handling address different layers of the AEM architecture.
Hi @user96222
In the context of Adobe Experience Manager (AEM), let's clarify the distinctions between EventListener, EventHandler, OSGi event handling, and Sling event handling:
EventListener vs. EventHandler:
OSGi Event Handling vs. Sling Event Handling:
Key Differences:
In summary, while there may be some overlapping terminology, it's essential to consider the context in which these terms are used within AEM. EventListeners and EventHandlers may be used interchangeably depending on the use case, while OSGi and Sling event handling address different layers of the AEM architecture.
Thanks
Hi @user96222
EventListener and EventHandler are two different interfaces for handling events in different contexts.
EventListener is used for handling JCR level events, while EventHandler is used for handling OSGi level events.
Here's an example of how to create an EventHandler:
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.EventHandler;
import org.osgi.service.event.Event;
@Component(service=EventHandler.class, immediate = true)
public class PracticeEventHandler implements EventHandler {
@Override
public void handleEvent(Event event) {
// handle the event here
}
}
On the other hand, here's an example of how to create an EventListener:
import org.apache.sling.api.resource.ResourceChangeListener;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceEvent;
import org.apache.sling.api.resource.ResourceEventListener;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@Component(service = ResourceChangeListener.class)
public class PracticeEventListener implements ResourceChangeListener {
private static final Logger LOG = LoggerFactory.getLogger(PracticeEventListener.class);
@Activate
@Modified
@Override
public void onChanged(List<ResourceEvent> events) {
// handle the events here
}
@Deactivate
@Override
public void onChanged(List<ResourceEvent> events, @SuppressWarnings("rawtypes") EventAdmin eventAdmin) {
// unregister the listener here
}
}
Regarding your question about Osgi event handling vs Sling event handling, both handle resource path but the major difference is that Sling event handling is built on top of Osgi event handling and provides additional features such as job handling.
Here's an example of how to create a job consumer in Sling:
import org.osgi.service.component.annotations.Component;
import org.apache.sling.event.jobs.JobConsumer;
import org.apache.sling.event.jobs.Job;
@Component(service=JobConsumer.class, property= {
JobConsumer.PROPERTY_TOPICS + "=my/special/jobtopic"
})
public class PracticeJobConsumer implements JobConsumer {
@Override
public JobResult process(Job job) {
// process the job here
return JobResult.OK;
}
}
Regarding scheduled jobs, they are put in the queue at a specific time (optionally periodically) using the ScheduleBuilder. Here's an example:
import org.apache.sling.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(immediate=true)
public class PracticeScheduledJob {
@Reference
private JobManager jobManager;
public void scheduleJob() {
ScheduleBuilder scheduleBuilder = jobManager.createJob("my/special/jobtopic").schedule();
// configure the schedule here
scheduleBuilder.build();
}
}
Thanks, even job handling can be done through osgi event as well right