April 1, 2020
Estimated Post Reading Time ~

How to use static agent in CQ / WEM

Use case:
1) You want to create a file system view of the repository on each activation.
2) You want to create a backup of your site in the file system (After that you can zip it and store it where ever you want)

Solution:
1) Login into your system and then go to http://host:port/libs/wcm/core/content/misc.html#/etc/replication/agents.author
2) Create a new replication agent of type "Static Delivery Agent"



3) Double click on newly created agent and click on edit. Enter all descriptions and click on the "Rules" tab.
4) You can enter the target repository (By default it is /tmp in Unix system). Make sure that you have sufficient rights to that folder.
5) In the definition section, specify for a specific path what rule you want to apply. For example if you have rule like
/content/geo* ${path}.html?wcmmode=preview this mean for any {path} activated that start with /content/geo* static agent will request http://host:port/{path}.html?wcmmode=preview and then store that content under {target path} you set above



6) Now we have to override default static setting from Felix console. For that please go to http://host:port/system/console/configMgr and click on "Static Content Builder". The default port there is 4502, You can change that according to your need.



7) Now after each activation static representation of content will get stored in {target path}

8) Note that the static agent gets the trigger on replicate action. So if you configure the static agent on publishing and activate page on the author, It will not get triggered. In order to trigger this on publish, Please create a static agent under agent.publish (http://HOST:PORT/miscadmin#/etc/replication/agents.publish) and then add "triggerModified" property to true using CRXDE light. You can also remove ?wcmmode=preview from publish, From a set of rules (Do not forget to change Felix configuration to point to publish instance)



Note that the modification event only works for cq:page. For all other modification to get trigger on publish, Please refer http://dev.day.com/content/kb/home/cq5/CQ5SystemAdministration/HowToFlushAssetsPublish.html

You might have to the entry of agent:static in this case.

Note that if you are making the above changes in autor, You have to activate static agents on publishing after making change.

You can write a code to iterate through all pages in the repository and then use replication API to activate those pages. This way you can create a file system snapshot of the whole repository. You can use the following code to call replication through replication,

com.day.cq.replication.Replicator replicator = sling.getService(Replicator.class) // in a JSP

OR

@Reference
com.day.cq.replication.Replicator replicator = null; // in an OSGi component

THEN

com.day.cq.replication.ReplicationOptions opts = new ReplicationOptions();
opts.setFilter(new com.day.cq.replication.AgentFilter() {
public boolean isIncluded(com.day.cq.replication.Agent agent) {
return agent.getId("static"); // the ID of the agent is the node name, e.g. "static" for /etc/replication/agents/static
}
}
); // filter by replication agent

THEN

replicator.replicate(javax.jcr.Session, ReplicationActionType.ACTIVATE, path, opts); // activate

Or

use following code to get all static replication agent

AgentManager agentMgr = sling.getService(AgentManager.class);
ArrayList allStaticAgent = new ArrayList();
for (Agent agent: agentMgr.getAgents().values()) {
if (agent.isEnabled()) {
if(agent.getId().equalsIgnoreCase("static"){
allStaticAgent.add(agent);
}
}

}
}

THEN

ReplicationOptions replicationOptions = new ReplicationOptions();
for(Agent agent:allStaticAgent){
replicationOptions.setFilter(new AgentIdFilter(agent.getId()));
replicator.replicate(javax.jcr.Session, ReplicationActionType.ACTIVATE, path, opts); // activate

}

Note For creating a file system view of DAM renditions or DAM Asset you might have to create some custom plugin.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.