Last active
December 3, 2021 13:42
-
-
Save trekawek/8998871 to your computer and use it in GitHub Desktop.
Handling events using JobManager. More info here: http://sling.apache.org/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?