Expand my Community achievements bar.

SOLVED

Event handler

Avatar

Level 4

EventListner vs Event handler any difference?

Osgi event handling vs sling event handling, both handle resource path any other major difference?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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:

  1. EventListener vs. EventHandler:

    • EventListener: This term is more generic and could be used in different contexts. In AEM, an EventListener is often associated with JCR (Java Content Repository) observation. In this scenario, it refers to a component that listens for changes in the JCR repository, such as node modifications, additions, or deletions.
    • EventHandler: This term can be more specific and may refer to components or scripts designed to handle events triggered within AEM. These events could be related to various activities, not just JCR observation. For example, handling custom events triggered by workflow steps or other AEM-specific activities.
  2. OSGi Event Handling vs. Sling Event Handling:

    • OSGi Event Handling: OSGi (Open Service Gateway Initiative) is a framework for modular application development in Java. In AEM, OSGi services can be used to handle events. OSGi events often deal with low-level runtime activities within the OSGi container.
    • Sling Event Handling: Sling is a web framework built on top of JCR, and it provides its own event handling mechanism. Sling events are more focused on high-level activities within the Sling framework, especially concerning resource-related events.

Key Differences:

  • OSGi events are more general and deal with events at the OSGi container level.
  • Sling events are more specific to the Sling framework and often related to resource-level activities.
  • EventListeners in AEM commonly refer to JCR observation, while EventHandlers can cover a broader range of event handling within AEM.

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.



Arun Patidar

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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:

  1. EventListener vs. EventHandler:

    • EventListener: This term is more generic and could be used in different contexts. In AEM, an EventListener is often associated with JCR (Java Content Repository) observation. In this scenario, it refers to a component that listens for changes in the JCR repository, such as node modifications, additions, or deletions.
    • EventHandler: This term can be more specific and may refer to components or scripts designed to handle events triggered within AEM. These events could be related to various activities, not just JCR observation. For example, handling custom events triggered by workflow steps or other AEM-specific activities.
  2. OSGi Event Handling vs. Sling Event Handling:

    • OSGi Event Handling: OSGi (Open Service Gateway Initiative) is a framework for modular application development in Java. In AEM, OSGi services can be used to handle events. OSGi events often deal with low-level runtime activities within the OSGi container.
    • Sling Event Handling: Sling is a web framework built on top of JCR, and it provides its own event handling mechanism. Sling events are more focused on high-level activities within the Sling framework, especially concerning resource-related events.

Key Differences:

  • OSGi events are more general and deal with events at the OSGi container level.
  • Sling events are more specific to the Sling framework and often related to resource-level activities.
  • EventListeners in AEM commonly refer to JCR observation, while EventHandlers can cover a broader range of event handling within AEM.

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.



Arun Patidar

Avatar

Level 4

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

 

 

 

 

Avatar

Level 4

Thanks, even job handling can be done through osgi event as well right