Tuesday, 30 April 2024

OSGI Factory Configuration

  • OSGI Factory Configuration

When you want to create more than One instance of any OSGI Configuration in that case you will create OSGI Factory Configuration.

for eg:-


  • How to make a OSGI Configuration as a OSGI Factory Configuration?

@Designate(ocd=Configuration name.class, factory=true)

  • we add factory=true parameter 

How to write OSGI Factory Configuration? 

  • Step 1: We can keep our configuration in a separate file
for example: com.adobe.aem.assets.asics.core.config



Step 2: write OSGI service Interface



Step 3: write OSGI service class



What is the meaning of 


  • Inside the @Reference annotation, we can define which service to inject using the OSGi filter configuration.
  • In OSGi, the cardinality of a reference is defined by the cardinality attribute of the @Reference annotation. It determines how many service instances can be bound to a component and how the component behaves when services become available or are withdrawn.
  • Without Multiple (Default Single Cardinality):

    If the cardinality is not specified or set to single, the component will bind to the first available service instance. Once bound, the component will not listen for additional service instances.

    With Multiple Cardinality:

    If the cardinality is set to Multiple, the component will listen for and bind to each service instance that becomes available. The binding method will execute every time a new service instance is created.

  • POLICY DYNAMIC:
  • If the policy is set to Static, OSGi will not dynamically manage the binding and unbinding of services. In this case, it will not automatically consider the unbind method.
  • If the policy is set to Dynamic, it means that the component can dynamically bind and unbind services. When your method is volatile and the policy is dynamic, OSGi will automatically find and use the unbind method, based on its name, to handle the unbinding of services.
How Bind and Unbind Work

  • Whenever a service is bound, the bind method is called. At bind time, if the configuration is created or available, it binds with the service. The first time, it creates a list, and each subsequent time, it adds the configuration to that list. Every time this method executes, it keeps adding to this list.
  • Every time a configuration is removed, the unbind method is triggered and removes that configuration from the list. Suppose you add a configuration; the bind method will execute and add the configuration to the list. If you remove that configuration, the unbind method will execute and remove the configuration from the list.
Step 4: Write Sling Model Interface


Step 5: call in sightly


Step 6: To Adding Configuration in Factory Configuration


How to view configuration on page




Thanks for Reading
Matri Sharma

Quotes 
"It takes courage to grow up and become who you really are." — 

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...