com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links' is unknown.

Caching Listener

WSO2 Carbon 4.4.x Kernel allows users to have 6 Cache entry listeners provided under the javax.cache.event package. These Cache entry listeners are listed below.

  • CacheEntryCreatedListener

  • CacheEntryExpiredListener

  • CacheEntryListener

  • CacheEntryReadListener

  • CacheEntryRemovedListener

  • CacheEntryUpdatedListener

These are the extension points provided in WSO2 carbon products for caching. These interfaces enable you to observe and take actions on the changes made to cache entries based on the relevant events. That is, if you need to perform some action when a cache entry is created, the CacheEntryCreatedListener interface can be implemented.

Once you implement a listener, for it to actually listen to the relevant event, you need to register your implementation on the cache. All the event handlers in these listeners gets CacheEntryEvent<? extends K, ? extends V> cacheEntryEvent object. So you can actually get the key and the value of the cache entry that triggers the particular event.

The following sample illustrates how to provide your custom implementation for these listeners and register them so that you can actually listen to these events. These implementations need to be bundled as an OSGi bundle and deployed for your Carbon product.

The process of implementing and deploying cache listeners are explained in the following sections:


Implementing cache listeners

This section illustrates sample implementations for the aforementioned listener classes. A bundle activator class will register these listeners into the cache and create an OSGi bundle out of the classes. Since the implementation process for all these 6 listeners is similar, this section illustrates only the implementation of CacheEntryCreatedListener.

package org.wso2.carbon.caching.test;

import javax.cache.event.CacheEntryCreatedListener;
import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryListenerException;

public class CacheEntryCreatedListenerImpl<K, V> implements CacheEntryCreatedListener<K, V> {
@Override
   public void entryCreated(CacheEntryEvent<? extends K, ? extends V> cacheEntryEvent) throws CacheEntryListenerException {
       //Your logic goes here
       System.out.println("Cache entry created");
       System.out.println("key: " + cacheEntryEvent.getKey() + ", value: " + cacheEntryEvent.getValue());
   }
}

Registering the listener

Once you implement the listener class, you need to register this listener in the cache. For this, let’s create a bundle activator class and register the listener as shown below.

package org.wso2.carbon.caching.test;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;

public class CachingBundleActivator implements BundleActivator {
   public static final String CACHE_NAME = "CacheListener-cache";
   private Cache<String, Long> cache;
   private CacheEntryCreatedListenerImpl<String, Long> cacheEntryCreatedListener;

   public void start(BundleContext bundleContext) throws Exception {
       CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager("test");
       cache = cacheManager.getCache(CACHE_NAME);

       cacheEntryCreatedListener = new CacheEntryCreatedListenerImpl<String, Long>();
       cache.registerCacheEntryListener(cacheEntryCreatedListener);      
   }

   public void stop(BundleContext bundleContext) throws Exception {

   }
}

Deploying the OSGi bundle

The following Maven plugin needs to be included in the pom.xml of your project to create an OSGi bundle.

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <version>1.4.0</version>
   <extensions>true</extensions>
   <configuration>
       <instructions>
           <Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
           <Bundle-Name>${pom.artifactId}</Bundle-Name>
           <Bundle-Version>1.0.0</Bundle-Version>
           <Export-Package>org.wso2.carbon.caching.test.*</Export-Package>
           <Bundle-Activator>org.wso2.carbon.caching.test.CachingBundleActivator</Bundle-Activator>
       </instructions>
   </configuration>
</plugin>
<plugin>

In addition to the aforementioned POM update, the packaging of your Maven project needs to be set to bundle (i.e: <packaging>bundle</packaging>). You can implement the remaining 5 listeners in a similar manner and create OSGi bundles. After building the OSGi bundle, add it to the <PRODUCT_HOME>/repository/components/dropins directory in your Carbon product.

com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.