Last updated on
18 May 2021
Question
How can I implement a Replication Event Listener?
Answer, Resolution
- Create a class that implements EventHandler interfaces.
- Add the following
SCR
annotations
@scr.component metatype="false" immediate="true" @scr.service interface="org.osgi.service.event.EventHandler" @scr.property name="event.topics" valueRef="ReplicationAction.EVENT_TOPIC"
- Implement
public void handleEvent(Event event)
method.
Example
This simple example adds a Replication Event Listener to Geometrixx application. All it does is log the events it receives.
- Open CRXDE
- Add the following class implementation. Make sure to save it in the correct folder under
/apps/geometrixx/src/impl/src/main/java
based on the package name.
package com.day.cq.wcm.apps.geometrixx.impl; import org.osgi.service.event.Event; import com.day.cq.workflow.event.WorkflowEvent; import org.osgi.service.event.EventHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.day.cq.replication.ReplicationAction; /** * This is an example listener that listens for replication events and * logs a message. * * @scr.component metatype="false" immediate="true" * @scr.service interface="org.osgi.service.event.EventHandler" * @scr.property name="event.topics" valueRef="ReplicationAction.EVENT_TOPIC" */ public class ReplicationEventListener implements EventHandler { /** * default logger */ private static final Logger log = LoggerFactory.getLogger(ReplicationEventListener.class); public void handleEvent(Event event) { String n[] = event.getPropertyNames(); log.info(""); log.info("Event occurred: {}", event.getProperty(WorkflowEvent.EVENT_TYPE)); log.info("Event properties: "); for(String s : n) { log.info(s + " = " + event.getProperty(s)); } ReplicationAction action = ReplicationAction.fromEvent(event); if(action != null) { log.info("Replication action {} occured on {} ", action.getType().getName(), action.getPath()); } log.info(""); } }
- Right click file
com.day.cq.wcm.cq-wcm-geometrixx.bnd
under/apps/geometrixx/src/impl/src/main/java
and selectBuild->Build Bundle
. This should build and restart geometrixx bundle in CQ instance your CRXDE is connected to.- Note, if you don't see
com.day.cq.wcm.cq-wcm-geometrixx.bnd
then right click onpom.xml
and selectBuild->Generate .bnd file
.
- Note, if you don't see
- To verify that your new event lister has been deployed go to the components tab in the Felix Console and make sure
com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener
is there.
To test your replication listener activate a page in CQ and examine crx-quickstart/logs/error.log file under your CQ installation. By default all logging from code deployed in CQ should go there. You should see messages similar to
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener Event occurred: null
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener Event properties:
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener event.topics = com/day/cq/replication
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener path = /content/geometrixx/en/news/webcasts
16.07.2010 13:01:32.525 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener type = ACTIVATE
16.07.2010 13:01:32.526 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener modificationDate = java.util.GregorianCalendar[time=1279310491837,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=16,DAY_OF_YEAR=197,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=1,SECOND=31,MILLISECOND=837,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]
16.07.2010 13:01:32.526 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener event.distribute =
16.07.2010 13:01:32.526 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener userId = admin
16.07.2010 13:01:32.526 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener Replication action Activate occured on /content/geometrixx/en/news/webcasts
16.07.2010 13:01:32.526 *INFO* [Thread-5560] com.day.cq.wcm.apps.geometrixx.impl.ReplicationEventListener
Applies To
CQ 5.3
Other Related Articles: