Skip to content

Instantly share code, notes, and snippets.

@trekawek
Last active December 3, 2021 13:42
Show Gist options
  • Save trekawek/8998871 to your computer and use it in GitHub Desktop.
Save trekawek/8998871 to your computer and use it in GitHub Desktop.
/* This OSGi service listens for events and creates a job for each one matching some conditions. */
@Component
@Service
@Properties({
// choose appropriate topic values
@Property(name = EventConstants.EVENT_TOPIC, value = {
SlingConstants.TOPIC_RESOURCE_CHANGED,
SlingConstants.TOPIC_RESOURCE_ADDED,
SlingConstants.TOPIC_RESOURCE_REMOVED
})
})
public class SampleEventHandler implements EventHandler {
@Reference
private JobManager jobManager;
@Override
public void handleEvent(Event event) {
String path = (String) event.getProperty(SlingConstants.PROPERTY_PATH);
String resourceType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE);
if (StringUtils.contains(path, "/jcr:content/configParsys/")) {
Map<String, Object> jobProperties = new HashMap<String, Object>();
// fill job properties map with useful values
jobProperties.put("path", path);
jobProperties.put("resourceType", resourceType);
jobManager.addJob(SampleJobConsumer.JOB_TOPIC, null, jobProperties);
}
}
}
/* This OSGi service consumes job created by the event listener above. */
@Component
@Service
@Properties({
@Property(name = JobConsumer.PROPERTY_TOPICS, value = SampleJobConsumer.JOB_TOPIC)
})
public class SampleJobConsumer implements JobConsumer {
public static final String JOB_TOPIC = "com/cognifide/cq/some/job/topic";
@Override
public JobResult process(Job job) {
String path = job.getProperty("path", String.class);
String resourceType = job.getProperty("resourceType", String.class);
// do something clever here
return JobResult.OK;
}
}
@mcwiise
Copy link

mcwiise commented May 20, 2016

How do you execute it?, I mean, who is the responsible to create an instance of SampleEventHandler so that a new job can be put into the queue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment