April 7, 2020
Estimated Post Reading Time ~

Create a OSGI Configuration Listener in AEM 6.1

Following article demonstrate business scenario where you need to execute a job, call a method or simply log when there in change in XYZ OSGI configuration. Taking forward the OSGI Configuration Management and Reading of OSGI Configuration, I will show you how you can create a OSGI Configuration listener. Before that’s take a look at below…

ConfigurationListener[Interface] objects are registered with the Framework service registry and are notified with a ConfigurationEvent object when an event is fired. ConfigurationListener objects can inspect the received ConfigurationEvent object to determine its type, the pid of the Configuration object with which it is associated, and the Configuration Admin service that fired the event.

I have used com.day.cq.wcm.notification.email.impl.EmailChannel service but you can use any service based on your requirements and will read property email.from when their is a change. Let’s get started..

1. Create a Service which Implement ConfigurationListener interface
@Component 
@Service 
public class OSGIConfigurationListener implements ConfigurationListener { }

2. Override ConfigurationEvent method and its implementation
@Override
public void configurationEvent(ConfigurationEvent event) {
 try {
  if (event.getPid().equals(OSGI_SERVICE_EMAIl_CHANNEL)) {
   readUpdatedConfig();
  }
 } catch (Exception exp) {
  log.info("Exception found while listening for change on {}, {} ", OSGI_SERVICE_EMAIl_CHANNEL, exp.getMessage());
 }
}

3. Invoke a custom define method to read required property 
         /**
          * Method will be executed once there is any change in the property of 
          * <code> com.day.cq.widget.impl.HtmlLibraryManagerImpl</code> service
          */
         public void readUpdatedConfig() {
          try {

           Configuration readConfig = configurationAdmin.getConfiguration(OSGI_SERVICE_EMAIl_CHANNEL);
           Dictionary < String, String > properties = readConfig.getProperties();
           log.info("Updated property is " + properties.get(EMAIL_FROM));

          } catch (IOException e) {
           log.info("Exception while reading property of {}, {}", EMAIL_FROM, e.getMessage());
          }

         }

4. Change the email.from property in service com.day.cq.wcm.notification.email.impl.EmailChannel


5. See the updated log file snippet



6. Here is complete code you can use 

package com.adobeaemclub.adobeaemclub.core.services;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationEvent;
import org.osgi.service.cm.ConfigurationListener;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Dictionary;

/**
 * Service will act as a listener for any configuration change and will show update property.
 * @author praveen
 *
 */
@Component
@Service
public class OSGIConfigurationListener implements ConfigurationListener {

 public static final Logger log = LoggerFactory.getLogger(OSGIConfigurationListener.class);

 public final static String OSGI_SERVICE_EMAIl_CHANNEL = "com.day.cq.wcm.notification.email.impl.EmailChannel";

 public final static String EMAIL_FROM = "email.from";

 @Reference
 ConfigurationAdmin configurationAdmin;

 @Override
 public void configurationEvent(ConfigurationEvent event) {
  try {
   if (event.getPid().equals(OSGI_SERVICE_EMAIl_CHANNEL)) {
    readUpdatedConfig();
   }
  } catch (Exception exp) {
   log.info("Exception found while listening for change on {}, {} ", OSGI_SERVICE_EMAIl_CHANNEL, exp.getMessage());
  }
 }

 /**
  * Method will be executed once there is any change in the property of 
  * <code> com.day.cq.widget.impl.HtmlLibraryManagerImpl</code> service
  */
 public void readUpdatedConfig() {
  try {

   Configuration readConfig = configurationAdmin.getConfiguration(OSGI_SERVICE_EMAIl_CHANNEL);
   Dictionary < String, String > properties = readConfig.getProperties();
   log.info("Updated property is " + properties.get(EMAIL_FROM));

  } catch (IOException e) {
   log.info("Exception while reading property of {}, {}", EMAIL_FROM, e.getMessage());
  }

 }
}


By aem4beginner

No comments:

Post a Comment

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