Saturday, August 19, 2023

AEM OSGI Configuration with Run Modes

 In this article we will learn about OSGI Configuration with run modes in Adobe Experience Manager 



Create OSGIConfiguration InterFace

Go to your maven project  > Core and create an Interface


Create OSGIConfiguration Implementation 

we will create a class here that will implement this interface

package sa.com.stc.core.services.Impl;
import org.osgi.service.metatype.annotations.*;
import sa.com.stc.core.services.CxpOsgiConfig;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;

@Component(service = CxpOsgiConfig.class,immediate = true)
@Designate(ocd = CxpOsgiConfigimpl.ServiceConfig.class )
public class CxpOsgiConfigimpl implements CxpOsgiConfig{

@ObjectClassDefinition(name="CXP - OSGi Configuration",
description = "OSGi Configuration demo.")
public @interface ServiceConfig {

@AttributeDefinition(
name = "Base Url",
description = "Enter Base Url.",
type = AttributeType.STRING)
public String baseURL() default "http://localhost:";

@AttributeDefinition(
name = "Port Number",
description = "Enter Port Number",
type = AttributeType.STRING)
public int portNumber() default 1234;
}

private String baseURL;
private int portNumber;

@Activate
protected void activate(ServiceConfig serviceConfig){
baseURL=serviceConfig.baseURL();
portNumber=serviceConfig.portNumber();
}

@Override
public String getBaseUrl() {
return baseURL;
}
@Override
public int getPort() {
return portNumber;
}
}



Annotations


@ObjectClassDefinition
In it we define the name of our OSGI Configuration with this name we can search this in aem console
http://localhost:4502/system/console/configMgr

Its is used create the fields on AEM instance 




@AttributeDefinition
it is used to define the type of type of field (it  will string or array or int) etc.



where it will save in CRXDLite

By default it will save in apps -> system -> config. and its values are coming from aem console.

but you can copy paste this in your project inside the different run mode folder also. if you not have run mode folders you can create them manually.
 


How you can run you instance with different run mode

stop your aem local server and type the mode in which you want to run it (no need to write author bcoz it is main run mode it will be auto selected)

java -jar aem-author.p4502.jar -r prod

you can check via bottom link which runmode is working



Important Points

when we will run simple author so it will use the OSGIConfiguration that you paste in author run mode
when we will run stage or other so it will come to  the OSGIConfiguration of author first, and will pick all values, then it will go to stage runmode, if it found here is some new values he will override only that value that is change here 

now you can use your osgi congiguration in backend in any module like (sling model , servlet etc)

How we can use OSGIConfiguration in sling model


Previous Post
Next Post

post written by:

0 comments: