OCD issue with @Activate annotation

In this blog, I will explain a very common error made by AEM developers during the development of OSGi Components. So most of the AEM developers those earlier used Felix SCR annotation for OSGi component creation, made this common mistake.

 

Here are some common issue statements said by AEM developers.

  1. Some developers said that during component activation configuration values are not getting applied firs time.
  2. Some developers said that their OSGi component is not working for the first time and after that, it is working fine.
  3. Some developers said that we are adding log message in @Activate method having @Modified annotation as well and the first time I am getting two log messages print in my log file. After that, I am getting one log message.
  4. Some developers say that-
    When we were creating a component using Felix SCR annotation we normally used @Properties annotation or @Property annotation for adding configurations. We used @Activate annotation on the method which we want to call at the time of component activation. when all code builds for the first time all the properties got populated with default values and it works. But now, we are using OSGi annotations in place of Felix SCR annotations. So, in OSGi components, we use OCD configurations for our component. But our code is not working as expected. We are not getting values at the time of activation after that we are getting the values.

 

Here is the reason for all of these issues-
OSGi components got installed first and activated before activation of these OCD configurations. So that @Activate method got called before the population of OCD configuration with default values. That’s why OSGi component found null or blank values for the first time.

The actual process of these OCD configurations activation is – First they got installed and activated and they got modified with default values. So if you add a log message in a method having @Activate and @Modified annotation than when the code got deployed you will see 2 log messages in the log file for the first time only. one for activation and second for modified event.

So if you want to do some configuration at the time of activation it will be triggered with empty or null values.

 

What is the best way to deal with it?
In place of @Activate annotation only use @Modified annotation on your method and build the code. Then you will see your method will work fine.

 

What if I want to perform a one-time activity at the time of code installation?
In this case, the developer can use normal java practice and can add a flag with true or false values. For the first time keep its value as false and once the @Modified method runs for the first time change it’s value to true and for next time check this flag value and don’t perform the same activity for next times.

 

Happy Coding
Namah Shivay