Expand my Community achievements bar.

Automate the process of flushing dispatcher

Avatar

Level 2

Hello,

 

  We have AMS managed AEM. Whenever the code is merged, the build process is automatically initiated and the code is deployed to cloud manager QA and we then manually flush dispatcher cache. Is there a way, where we can automate the dispatcher flushing in non-production pipeline-QA?

 

Any suggestions are appreciated.

Thanks in advance!

 

Ram

7 Replies

Avatar

Community Advisor

Refer below-

 

Example flush servlet

The following code implements a servlet that sends an invalidate request to Dispatcher. The servlet receives a request message that contains handle and page parameters. These parameters provide the value of the CQ-Handle header and the path of the page to recache, respectively. The servlet uses the values to construct the HTTP request for Dispatcher.

When the servlet is deployed to the publish instance, the following URL causes Dispatcher to delete the /content/geometrixx-outdoors/en.html page and then cache a new copy.

10.36.79.223:4503/bin/flushcache/html?page=/content/geometrixx-outdoors/en.html&handle=/content/geometrixx-outdoors/en/men.html

NOTE

This example servlet is not secure and only demonstrates the use of the HTTP Post request message. Your solution should secure access to the servlet.

package com.adobe.example;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

@Component(metatype=true)
@Service
public class Flushcache extends SlingSafeMethodsServlet {

 @Property(value="/bin/flushcache")
 static final String SERVLET_PATH="sling.servlet.paths";

 private Logger logger = LoggerFactory.getLogger(this.getClass());

 public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  try{ 
      //retrieve the request parameters
      String handle = request.getParameter("handle");
      String page = request.getParameter("page");

      //hard-coding connection properties is a bad practice, but is done here to simplify the example
      String server = "localhost"; 
      String uri = "/dispatcher/invalidate.cache";

      HttpClient client = new HttpClient();

      PostMethod post = new PostMethod("https://"+host+uri);
      post.setRequestHeader("CQ-Action", "Activate");
      post.setRequestHeader("CQ-Handle",handle);
   
      StringRequestEntity body = new StringRequestEntity(page,null,null);
      post.setRequestEntity(body);
      post.setRequestHeader("Content-length", String.valueOf(body.getContentLength()));
      client.executeMethod(post);
      post.releaseConnection();
      //log the results
      logger.info("result: " + post.getResponseBodyAsString());
      }
  }catch(Exception e){
      logger.error("Flushcache servlet exception: " + e.getMessage());
  }
 }
}
Copy
 

Avatar

Level 2

Thanks Ankur, I have seen this code in adobe documentation. I want to flush the dispatcher content when a package is installed on publish instance. What could be the best possible solution? Is it a workflow launcher or event listener or something else?

 

Thanks,

Ram

Avatar

Community Advisor

Hi @RamTejaReddy ,

     You need to use workflow launcher whenever package installed.

 

Kr,

Sanjay

  

Avatar

Community Advisor

Hi @RamTejaReddy  When you say package is installed on the publish instance, are you talking about the code package or any kind of content package?

 

If it's a code package, then you can add a shell script in the Jenkins which can run after the deployment step and can flush the dispatcher cache based on the path that you have configured.

 

Thanks!

Avatar

Community Advisor

Hi @RamTejaReddy 

 

Please check below blog which has info on shell script and how to configure Jenkins job for auto cache clear. 

https://aem6solutions.blogspot.com/2019/11/cache-clearance-in-dispatcher.html 

 

Thanks

Avatar

Level 3

Hey @RamTejaReddy how do you flush the dispatcher manually? Can you sahre that info please?

Thanks in advance.

Avatar

Level 2

Hi @israel_sanchez you can manually flush the dispatcher cache by the following path in any environment: /etc/acs-commons/dispatcher-flush/complete-flush.html.

 

Hope this helps.

 

Thanks,

Ram