Monday, January 15, 2018

Developing a caching service to cache third part elements to improve performance in Adobe Experience Manage(AEM)

Developing a caching service to cache third part elements to improve performance in Adobe Experience Manage(AEM) 


This post will explain the approach to cache third party elements to improve performance.
In AEM sometime we may have to load the third party data e.g. country list, state list to drop-down lists or other components from external service call, the response will be static and changes occasionally over time.

To improve the performance the data can be cached in AEM and enable a JMX bean to clear the cache whenever the data is modified in the external system. This approach can be modified according to the requirement, if the volume of the cache data is more then better to integrate with external caching system like ehcache.

Define the cache manager to put/get the cache objects
Define JMX bean to clear the cache objects

1. Define  CacheManager  to put/get objects into cache and methods to clear the cache through JMX bean

public class CacheManager {

  private static java.util.HashMap<String,Object> cacheHashMap = new java.util.HashMap<String,Object>();

  public static void putCache(Object object, String identifier)
  {
    cacheHashMap.put(identifier, object);
  }

  public static Object getCache(String identifier)
  {
  Object object = cacheHashMap.get(identifier);  
      return object;  
  }

   public static String removeCacheWithKey(String identifier)
  {
    if(!cacheHashMap.containsKey(identifier))
    {
    return "Invalid Key..";
    }
cacheHashMap.remove(identifier);  
return "Removed cache with key "+identifier;
 
  }

   public static String removeCacheAll()
  {
    cacheHashMap.clear();
    return "Removed cache";
 
  }

}


2. Sample client to get/put object into cache

public class CacheCleint {

public static void main(String[] args) {

String[] countrylist=new String[]{"USA","UK","AUS","IN"};

CacheManager.putCache(countrylist, "country-list");

String[] cachedCountrylist=(String[])CacheManager.getCache("country-list");

System.out.println(cachedCountrylist[1]);
}

}

3. Define JMX mbean to clear the cache

Define JMX mbean that will help to clear cache from OSGI JMX console or other JMX client.

import com.adobe.granite.jmx.annotation.Description;
import com.adobe.granite.jmx.annotation.Name;

@Description("Cache Manager JMX Bean")
public interface CacheManagerMBean {

    @Description("Remove the cache with cache id - country-list,state-list")
    String removeCacheWithKey(@Name("cacheKey") @Description("Valid cache key") String Cachekey);

    @Description("Remove All Cache")
    String removeCacheAll();
}

import javax.management.NotCompliantMBeanException;

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

import com.adobe.granite.jmx.annotation.AnnotatedStandardMBean;

@Component(immediate = true)
@Property(name = "jmx.objectname", value = "com.packagegenerator.core:type=CacheManager")
@Service
public class CacheManagerMBeanImpl extends AnnotatedStandardMBean implements CacheManagerMBean {

    public CacheManagerMBeanImpl() throws NotCompliantMBeanException {
        super(CacheManagerMBean.class);
    }

    @Override
    public String removeCacheWithKey(String cacheKey) {
    return CacheManager.removeCacheWithKey(cacheKey);
    }

    @Override
    public String removeCacheAll() {   
    return CacheManager.removeCacheAll();   
    }
 
}

The cache objects specified to a key or complete cache can be cleared through JMX client.

This will enable to cache the static data from a external system into AEM and improve the performance, the cache can be cleared through JMX console whenever required.

jmx-bean-cache-clear

jmx-bean-cache-clear

jmx-bean-cache-clear

jmx-bean-cache-clear

Sunday, January 14, 2018

How to upload and install the packages through Java API - Adobe Experience Manager(AEM)

How to upload and install the packages through Java API - Adobe Experience Manager(AEM)


This post will explain the approach to upload and install the packages through JAVA.

Refer the post for details on creating the package through Java - https://www.albinsblog.com/2018/01/sling-scheduler-to-auto-create-packages-JcrPackageManager-api-aem.html

Sample Servlet to upload and install the package - Convert the servlet to scheduler and import/replicate the package based on the scheduled time

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.jackrabbit.vault.fs.io.ImportOptions;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.Replicator;

/**
 * Servlet that writes some sample content into the response. It is mounted for
 * all resources of a specific Sling resource type. The
 * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
 * idempotent. For write operations use the {@link SlingAllMethodsServlet}.
 */
@SuppressWarnings("serial")
@SlingServlet(paths= "/services/packageupload")
public class PackageUploadServlet extends SlingSafeMethodsServlet {

@Reference
private Packaging packaging;

@Reference
private SlingRepository repository;

@Reference
private ResourceResolverFactory resolverFactory;

@Reference
private SlingSettingsService settingsService;

@Reference
Replicator replicator;

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {

Session session = null;
if (isRunMode("author") && isMasterRepository()) {//Only run in author - change the servlet to scheduler and import/replicate the package based on the scheduled time
try {

String packagePath = "C:\\Albin\\test12018-01-14-15-00-00-023-1.0.zip";
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "packageService");

ResourceResolver resolver;

resolver = resolverFactory.getServiceResourceResolver(param);

session = resolver.adaptTo(Session.class);
JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);

JcrPackage inputPackage=jcrPackageManager.upload(new File(packagePath), false, true, "test12018-01-14-15-00-00-023-1.0");
ImportOptions importOption=new ImportOptions();//Specify the import configurations

inputPackage.install(importOption);

//Replicate the package to publishers
replicator.replicate(session, ReplicationActionType.ACTIVATE, inputPackage.getDefinition().getNode().getPath().split("/jcr:content")[0]);

resp.getOutputStream().println("This package successfully uploaded and installed to the server....");

} catch (Exception e) {
// TODO Auto-generated catch block
resp.getOutputStream().println("Excpetion while uploading/installing the package...."+e.getMessage());
e.printStackTrace();
}finally {
if(session!=null)
{
session.logout();
}
}
}
}

private Boolean isRunMode(String mode) {
Set<String> runModes = settingsService.getRunModes();
for (String runMode : runModes) {
if (runMode.equalsIgnoreCase(mode)) {
return true;
}
}
return false;
}

public boolean isMasterRepository() {
final String isMaster = repository.getDescriptor("crx.cluster.master");
return isMaster != null && !isMaster.equals("") && Boolean.parseBoolean(isMaster);
}
}

Sling scheduler to auto create the packages through JcrPackageManager API - Adobe Experience Manager(AEM)

Sling scheduler to auto create the packages through JcrPackageManager API - Adobe Experience Manager(AEM)


This post will explains the approach to define Sling scheduler to auto create the packages through JcrPackageManager API in Adobe Experience Manager(AEM).

Define a Service user:


Define a Service User with required access to create a  package.

Login to CRX explorer -  http://localhost:4502/crx/explorer/index.jsp
Click on User Administration

AEM-service-user

Click on Create System User

AEM-service-user

Enter the User Id - "packageassembler and the path - "system/packageassembler"

AEM-service-user