Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How check the number of Queue in ReplicationQueue in AEM6.5

Avatar

Level 2

Hi All,

I need to check the number of Queues in replicationQueue before proceeding the activate or deactivate in a workflow if the number of queue in replication will be greater than 50 then it shoud not allow to trigger the workflow , how can I approach this?

 

@Override
public void execute (WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
            String payload = workItem.getWorkflowData().getPayload().toString();
            if (rootPage != null && replicationQueueChechk()) {
                if (StringUtils.equalsIgnoreCase(comments, MOVE_TO_DEV_UNPUBLISH)) {
                    replicationOptions.setFilter(agent -> agent.getId().matches(".*(?<!agent)$"));
                }
                if (StringUtils.equalsIgnoreCase(comments, MOVE_TO_QA_UNPUBLISH)) {
                    replicationOptions.setFilter(agent -> agent.getId().endsWith("agent"));
                }
                try {
                    replicator.replicate(session, ReplicationActionType.DEACTIVATE, payload, replicationOptions);
                } catch (ReplicationException e) {
                    loggerService.postLog(LogLevel.ERROR, "Replication exception occurred at :: {}" + Arrays.toString(paths) + " and the error message is :: {}" + e.getMessage());
                }
            }
}
private boolean replicationQueueChechk () {
            //TODO check the number of active objects in replication queue, return true only if replication queue is less than 500, otherwise return false

}

 


I tried to follow multiple documents like : https://javadoc.io/static/com.adobe.aem/aem-sdk-api/2023.4.11873.20230421T153841Z-230200/com/day/cq/... 

But could not able to acieve this.

Could you please guide me the approach.

 

tried solution : 

//Get the replication queue from the session object
//Get the entries list from the replication queue.
//Get the size of the entries, and then de-activate based on it.
private boolean replicationQueueChechk (Session session) {
ReplicationQueue replicationQueue = Replication.getQueue(resourceResolver.adaptTo(session.class));  List<ReplicationQueueEntry> queueEntries = replicationQueue.entries();
 if (queueEntries.size() < 500) { 
// If yes, then deactivate the pages
} else {
//some message
}
}

or

private boolean replicationQueueCheck() {
    ReplicationQueue replicationQueue = session.adaptTo(ReplicationQueue.class);
    int queueSize = replicationQueue != null ? replicationQueue.getQueueEntries().size() : 0;
    return queueSize < 500;
}

private boolean isReplicationQueueUnderLimit(int limit) {
    ReplicationQueue replicationQueue = session.adaptTo(ReplicationQueue.class);
    int queueSize = replicationQueue != null ? replicationQueue.getQueueEntries().size() : 0;
    return queueSize < limit;
}

or

public int getQueueCount(ResourceResolver resourceResolver, String agentId) throws SlingException {
        ReplicationAgentManager agentManager = resourceResolver.adaptTo(ReplicationAgentManager.class);
        ReplicationQueue replicationQueue = agentManager.getQueue(agentId);

        if (replicationQueue != null) {
            java.util.List<QueueEntry> queueEntries = replicationQueue.getQueueEntries();
            return queueEntries.size();
        }

        return 0;
    }

 

Thanks

 

@kautuk_sahni  @arunpatidar  @lukasz-m @Anmol_Bhardwaj 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Marcos_aem,

If you want to check number of items queued for replication you should use AgentManager service that will give you list of all the agents.It can be used to check size of replication queue for each agent. Because there could be multiple agents responsible for publication (e.g. when you have more than one publish instance) you have to check all of them.

You can try below sample code, which in general do following things:

  1. Gets all the agents.
  2. Filter agents against list of publish agents.
  3. Check each agent replication queue size.

 

import com.day.cq.replication.*;
import org.osgi.service.component.annotations.Reference;

// getting agent manager object
@Reference
private AgentManager agentManager;

// list of publish instance ids - fill this list your agent ids
private static final List<String> publishAgentIds = Arrays.asList("publish_1", "publish_2");

// place for other code

// returns false if queue of at least one agent contains 500 or more items
private boolean replicationQueueCheck() {
    boolean result = true;
    for (Agent agent : getAgents()) {
        // checking if queue size is bigger than 500 if so, false will be returned, in other case, next agent will be verified
        if (agent.getQueue().entries().size() >= 500) {
            result = false;
            break;
        }
    }
    return result;
}

private List<Agent> getAgents() {
    List<Agent> agents = new ArrayList<Agent>();
    for (Agent agent : agentManager.getAgents().values()) {
        // filtering agents by ids, and selecting only publish related agents
        if (agent.isEnabled() && agent.isValid() && publishAgentIds.contains(agent.getId())) {
            agents.add(agent);
        }
    }
    return agents;
}

 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Marcos_aem,

If you want to check number of items queued for replication you should use AgentManager service that will give you list of all the agents.It can be used to check size of replication queue for each agent. Because there could be multiple agents responsible for publication (e.g. when you have more than one publish instance) you have to check all of them.

You can try below sample code, which in general do following things:

  1. Gets all the agents.
  2. Filter agents against list of publish agents.
  3. Check each agent replication queue size.

 

import com.day.cq.replication.*;
import org.osgi.service.component.annotations.Reference;

// getting agent manager object
@Reference
private AgentManager agentManager;

// list of publish instance ids - fill this list your agent ids
private static final List<String> publishAgentIds = Arrays.asList("publish_1", "publish_2");

// place for other code

// returns false if queue of at least one agent contains 500 or more items
private boolean replicationQueueCheck() {
    boolean result = true;
    for (Agent agent : getAgents()) {
        // checking if queue size is bigger than 500 if so, false will be returned, in other case, next agent will be verified
        if (agent.getQueue().entries().size() >= 500) {
            result = false;
            break;
        }
    }
    return result;
}

private List<Agent> getAgents() {
    List<Agent> agents = new ArrayList<Agent>();
    for (Agent agent : agentManager.getAgents().values()) {
        // filtering agents by ids, and selecting only publish related agents
        if (agent.isEnabled() && agent.isValid() && publishAgentIds.contains(agent.getId())) {
            agents.add(agent);
        }
    }
    return agents;
}