March 30, 2020
Estimated Post Reading Time ~

Run Modes with Use Cases In AEM 6.2 - Part2

In this blog we will explore the purpose of Run Modes in detail:
  • OSGi Component Creation for a specific environment.
  • Bundle Creation for a specific environment.
OSGi Component Creation for a specific environment:
Run Modes helps to create the OSGi component for the specific environment.
Problem Statement: To make OSGi Component be available only for publish instance.
Solution: To solve this problem, follow these two steps:
  • Annotate the component with “ConfigurationPolicy.REQUIRE
  • Make this configuration available under config.publish.

Sample code of OSGi Component
package com.aem.sgaem.project.services; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; import org.apache.felix.scr.annotations.Service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component(label = "SGAEM - OSGi Configuration for Publish Run Mode", immediate = true, policy = ConfigurationPolicy.REQUIRE) @Service(ComponentPublish.class) public class ComponentPublish { private static final Logger LOGGER = LoggerFactory.getLogger(ComponentPublish.class); @Activate protected void activate() { LOGGER.debug("Run Modes Activated"); } }

Here “policy= ConfigurationPolicy.REQUIRE”  means that this component should be available as configuration created under config nodes. But Initially, this configuration will not exist  in felix console components
So, to make it visible in Felix console of publish instance, You need to create a configuration of this component (using Pid of OSGi Component) under config.publish node.
component-config.jpg
Fig- Node name same as Pid of OSGi Component

After the addition of this configuration, you can see, the component will be available in Felix console in publish instance.
Note: In other run modes, this component will not get executed because it is specified only for publish instance. If you want to use it in other run modes, you need to make it in other configs as well. (eg: config.author,config.qa)


Bundle Creation for a specific environment:

Run Modes is not only used for configurations, but it can also be used to differentiate bundles based on the environments.
Isn’t it sound interesting??
Problem Statement: Let Suppose, You have multiple bundles in a single project. like
Bundle A” for  “Author” Environment.
Bundle Bfor  “Publish” Environment
Solution: So we need to make environment-specific configuration for an install folder.
Install.author  (nt:folder)
                => Bundle A.jar
Install.publish (nt:folder)
                => Bundle B.jar
crx.PNG
Fig- sgaem-test.jar for author and sgaem-bundle.jar for publishing instance
Note: If you are in publish instance, You will not able to see bundle "sgaem-test" in felix console and vice-Versa for the author instance.

Note: Here the question arises that how to create multiple bundles in an AEM Maven project? So stay tuned for the next upcoming blog to get the answer of the above question 👍.


By aem4beginner

No comments:

Post a Comment

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