Sling Content Distribution in AEM 6.5.21 | Community
Skip to main content
October 24, 2024
Solved

Sling Content Distribution in AEM 6.5.21

  • October 24, 2024
  • 1 reply
  • 1262 views

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?


This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

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;

@8220494(
service = EventHandler.class,
immediate = true,
property = {
"event.topics=org/apache/sling/distribution/event/DistributionEvent/END"
}
)
public class CustomReplicationEventListener implements EventHandler {

@3214626
private ResourceResolverFactory resourceResolverFactory;

@9944223
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
}
}
}

1 reply

SureshDhulipudi
Community Advisor
Community Advisor
October 24, 2024

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

 

October 25, 2024

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?

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
October 25, 2024

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;

@8220494(
service = EventHandler.class,
immediate = true,
property = {
"event.topics=org/apache/sling/distribution/event/DistributionEvent/END"
}
)
public class CustomReplicationEventListener implements EventHandler {

@3214626
private ResourceResolverFactory resourceResolverFactory;

@9944223
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
}
}
}