Here is my use case
1. Trigger an event on author on publish (We wrote an eventHandler that listens into EVENT_TOPIC). This is getting triggered
2 . Trigger a job from above event. We don't see the job being triggered. Below is my code
Do we need setup the topic manually in Felix console, our would it be created automatically once the OSGI bundle is deployed ? I don't see the topic getting created under Local topic consumers at the URL http://localhost:4502/system/console/slingevent
Event Handler class
-------------------------
package com.sp.core.events;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.osgi.service.event.EventConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.day.cq.replication.ReplicationAction;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
@Component(label = "content notification Event Handler", description = "Listen to repository replication event in order to notify SP", immediate = true, metatype = true)
@Service(value = { SpEventHandler.class, EventHandler.class })
@Properties({
@Property(name = EventConstants.EVENT_TOPIC, value = { ReplicationAction.EVENT_TOPIC }, label = "Event topic"),
@Property(name = "allowedPaths", label = "Allowed Paths", description = "Paths for which content approval Workflow should be triggered", value = {
"/content/servicing/sp/us" }, unbounded = PropertyUnbounded.ARRAY, cardinality = 50) })
public class SpEventHandler implements EventHandler {
private static final Logger log = LoggerFactory.getLogger(SpEventHandler.class);
@Reference
private ResourceResolverFactory resolverFactory;
private static final String SLASH_SEPARATOR = "/";
private static final String JCR_CONTENT = "jcr:content";
private static final String CQ_TEMPLATE_TO_MATCH = "/apps/sp/templates/content-serv-comp";
private static final String CONTENT_TYPE_TEMPLATE = "cq:template";
private String[] allowedPaths;
private static final String ALLOWED_PATHS = "allowedPaths";
@Reference
private JobManager jobManager;
@Override
public void handleEvent(final Event event) {
//if (isInstanceInMode(INSTANCE_MODE.AUTHOR)) {
// processJob(event, this);
// }
log.info("We got an event..");
ReplicationAction action = ReplicationAction.fromEvent(event);
log.info("Processing event forrr {}", action.getPath());
log.info("Event type isss {}", action.getType().getName());
Map<String,Object> jobData = new HashMap<String,Object>();
jobData.put("path", action.getPath());
jobData.put("type", action.getType().getName());
jobData.put(ALLOWED_PATHS, allowedPaths);
log.info("Before sending it to Job Manager..");
Job jobcreated = jobManager.addJob(com.metlife.gssp.metlife_GSSP.core.job.NotifyGsspJob.NOTIFY_GSSP_JOB_TOPIC, jobData);
if(jobcreated!=null) {
log.info("Job is created..State is.."+jobcreated.getJobState().toString());
//log.info("Job is created..State is.."+jobcreated.getQueueName()
}
}
@Activate
protected void activate(Map<String, Object> properties) {
this.allowedPaths = PropertiesUtil.toStringArray(properties.get(ALLOWED_PATHS));
}
/*
* This method is used to get the workflowModel from configurations
*/
@Modified
protected void modified(ComponentContext context) {
this.allowedPaths = PropertiesUtil.toStringArray(context.getProperties().get(ALLOWED_PATHS));
}
}
Job class
------------
package com.sp.core.job;
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.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.metlife.gssp.metlife_GSSP.core.HttpCaller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
//Sling Imports
/**
* NotifySpJob -
*
* @author BP
*/
@Component(label = "Notify SP Job", description = "Job that noitifies SP once content is activated or deactivated", metatype = true, immediate = true)
@Service(value={NotifySpJob.class})
@Property(name=JobConsumer.PROPERTY_TOPICS, value=NotifyGsspJob.NOTIFY_SP_JOB_TOPIC)
public class NotifySpJob implements JobConsumer {
private static final Logger log = LoggerFactory.getLogger(NotifySpJob.class);
public static final String NOTIFY_SP_JOB_TOPIC = "com/sp/core/job/NotifyGsspJob";
@Reference
private ResourceResolverFactory resolverFactory;
public JobResult process(final Job job) {
log.info("First thing in job manager..");
String resourcePath = (String)job.getProperty("path");
String eventType = (String)job.getProperty("type");
allowedPaths = (String[])job.getProperty("ALLOWED_PATHS");
log.info("Printing the path.."+resourcePath);
log.info("==========================================");
return JobResult.OK;
}
}