When content is distributed using SCD (Distribute feature available on /libs/granite/distribution/content/distribution-agent.html?agentName=publish), why does it not update the cq:lastReplicated, cq:lastReplicatedBy properties?
Solved! Go to Solution.
Views
Replies
Total Likes
please try like this:
Create an Event Listener: Implement an event listener to listen for content distribution events.
Update Properties: Update the cq:lastReplicated and cq:lastReplicatedBy properties in the event listener.
package com.example.aem.core.listeners;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import java.util.Calendar;
@component(
service = EventHandler.class,
immediate = true,
property = {
"event.topics=org/apache/sling/distribution/event/DistributionEvent/END"
}
)
public class CustomReplicationEventListener implements EventHandler {
@reference
private ResourceResolverFactory resourceResolverFactory;
@Override
public void handleEvent(Event event) {
String path = (String) event.getProperty("path");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null)) {
Resource resource = resolver.getResource(path);
if (resource != null) {
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
if (properties != null) {
properties.put("cq:lastReplicated", Calendar.getInstance());
properties.put("cq:lastReplicatedBy", resolver.getUserID());
resolver.commit();
}
}
} catch (Exception e) {
// Handle exception
}
}
}
When content is distributed using Sling Content Distribution (SCD) in AEM, the cq:lastReplicated and cq:lastReplicatedBy properties are not updated because SCD operates differently from the traditional replication mechanism. SCD is designed for distributing content across different Sling instances without invoking the replication process that updates these properties.
Traditional Replication: When content is replicated using the traditional replication agents, the cq:lastReplicated and cq:lastReplicatedBy properties are updated to reflect the time and user who performed the replication.
Sling Content Distribution (SCD): SCD focuses on distributing content efficiently across multiple instances. It does not trigger the same events or workflows as traditional replication, hence it does not update the cq:lastReplicated and cq:lastReplicatedBy properties.
Solution
Thanks for the response Suresh.
We have the customization done using event handlers on replication.. All of those have to be accommodated for SCD as well.. Pls share some information on to how to go abt this?
please try like this:
Create an Event Listener: Implement an event listener to listen for content distribution events.
Update Properties: Update the cq:lastReplicated and cq:lastReplicatedBy properties in the event listener.
package com.example.aem.core.listeners;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import java.util.Calendar;
@component(
service = EventHandler.class,
immediate = true,
property = {
"event.topics=org/apache/sling/distribution/event/DistributionEvent/END"
}
)
public class CustomReplicationEventListener implements EventHandler {
@reference
private ResourceResolverFactory resourceResolverFactory;
@Override
public void handleEvent(Event event) {
String path = (String) event.getProperty("path");
try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null)) {
Resource resource = resolver.getResource(path);
if (resource != null) {
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
if (properties != null) {
properties.put("cq:lastReplicated", Calendar.getInstance());
properties.put("cq:lastReplicatedBy", resolver.getUserID());
resolver.commit();
}
}
} catch (Exception e) {
// Handle exception
}
}
}
Views
Likes
Replies
Views
Likes
Replies