Sling Event Listening Code not working

Question!

I would like to have practical knowledge on Sling Eventing so i was trying to write the code. I have written a code but my sling eventing is not working. I can see my bundle active in osgi felix console but it's not listening the event.

Any suggestions .

By : Naveen


Answers
package com.adobe.training.core;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobProcessor;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

@SuppressWarnings("deprecation")
@Service(value = EventHandler.class)
@Component(immediate = true)
@Property(name = "event.topics", value = ReplicationAction.EVENT_TOPIC)
public class ReplicationLogger implements EventHandler, JobProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationLogger.class);
    @Reference
    private JcrResourceResolverFactory jcrResourceResolverFactory;
    @Override
    public void handleEvent(Event event) {
        LOGGER.info("********handling event");
        process (event);
    }
    @Override
    public boolean process(Event event) {
        LOGGER.info("********processing job");
        ReplicationAction action = ReplicationAction.fromEvent(event);
        ResourceResolver resourceResolver = null;
        if (action.getType().equals(ReplicationActionType.ACTIVATE)) {
            try {
                resourceResolver = jcrResourceResolverFactory.getAdministrativeResourceResolver(null);
                final PageManager pm = resourceResolver.adaptTo(PageManager.class);
                final Page page = pm.getContainingPage(action.getPath());
                if(page != null) {
                    LOGGER.info("********activation of page {}", page.getTitle());
                }
            }
            catch (LoginException e) {
                e.printStackTrace();
            }
            finally {
                if(resourceResolver != null 
By : sunz


I am guessing you don't have bundle activate() method. We use the "@Activate" annotation to indicate it.

import org.apache.felix.scr.annotations.Activate
...
    @Activate
    void activate(ComponentContext context) {...}
By : IT Gumby


This video can help you solving your question :)
By: admin