Set OSGi Component properties dynamically using OCD - Use case with Sling ResourceChangeListener

This post demonstrates the use of Object Class Definition(OCD) in setting the OSGi component properties.


In general, per OSGi R6 annotations and above, we can set OSGi component properties via

  • property attribute of @Component annotation (Static values) or
  • Dynamically using @ObjectClassDefinition together with @AttributeDefinition  (property amends in run time via Config Admin or /system/console/configMgr)
    • AttributeDefinition should be framed such that it evaluates to the property name correctly (underscores are converted to dots/period)
    • Association of OCD with OSGi component happens via annotation named @Designate

Example : Component properties like service.ranking and in case of specific implementations like say WorkflowProcess, we set property named process.label and for EventHandler, it is EventConstants.EVENT_TOPIC and examples add on in a similar fashion for any OSGi Component for that matter.  

OSGi Component Properties
In order to illustrate this, I would like to consider Sling ResourceChangeListener which expects the PATHS (mandatory property) to which it should listen to and optional CHANGES (added/removed/changed) and PROPERTY_NAMES_HINT (property name on the resource)

For static property values, we would write an Implementation for Listener like below. Sling ResourceChangeListener

Now for amending properties dynamically via Config Admin, we can create OCD in a separate file or within the same OSGi component.
In this case, ResourceChangeListener.PATHS is a field for the constant value - resource.paths. Hence @AttributeDefinition would be resource_paths(). Same way for other two properties.

Complete code snippet : 


On a long run, if we are to move to AEM as a Cloud service, this use case of dynamically amending properties via Config Admin/run time changes will not be applicable. However, if there is a situation of run mode specific OSGi component properties needs, then we can create OSGi config like we usually do for custom properties. (code level changes/ no run time)

Using the same example, 
  • Generic config listens for change of jcr:lastModified property on specified Asset resource. 
  • Author listens for change of cq:lastModified property on specified Page resource

OSGi Config - ResourceChangeListener


OSGi Config - ResourceChangeListener


I had a chance to respond to similar query in the Community, sharing the respective thread for reference -  https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/multiple-paths-handling-in-eventhandler-not-working/m-p/426921

Comments

  1. Coin Development Company Coin Development Company is a trusted partner in cryptocurrency development, offering expert team, tailored solutions, innovative technology, security, and comprehensive support. We specialize in coin creation, token development, smart contracts, and blockchain consulting. Our team focuses on staying ahead of the curve, ensuring your coin stands out in the market, and providing ongoing maintenance and updates to keep your coin running smoothly.

    ReplyDelete
  2. "This is a very insightful post! Understanding how to use OCD dynamically with OSGi components makes customization much easier."
    Spices Distributorship
    Chinese Restaurant Franchise

    ReplyDelete
  3. "Thanks for explaining the process with such clarity. Sling ResourceChangeListener integration is a great example!"
    Centrifugal Blowers
    Rotary Air Locks in India

    ReplyDelete
  4. "I was looking for a practical use case of dynamically setting OSGi properties—this article is perfect!"
    structural-steel tubes distributors gurgaon
    MS sheet distributors in Indore

    ReplyDelete
  5. "Great article! Can you also elaborate on how this approach compares to using static configurations?"
    tubular screw conveyorfeeder in rudrapur
    vibro sifter gyro screens in rudrapur

    ReplyDelete
  6. "Dynamic property setting in OSGi opens up so many possibilities. Thanks for sharing this example!"
    cyclone type dust collector ludhiana
    axial flow fans bawal

    ReplyDelete
  7. "The step-by-step breakdown is very helpful. It simplifies an otherwise complex topic."
    Franchise show Delhi
    modular office furniture gurgaon

    ReplyDelete
  8. "Would love to see more real-world examples of where this method can be applied."
    cnc wood cutting machine in delhi
    industiral conveyor

    ReplyDelete
  9. "This post clarified a lot of confusion I had about OSGi Component configurations. Thanks!"
    oven conveyor system manufacturer
    PVC Conveyor belt Manufacturer

    ReplyDelete
  10. "Amazing write-up! Can this method also be applied to non-Sling-related OSGi components?"
    mezzanine floor faridabad
    heady duty rack bhiwadi

    ReplyDelete
  11. "Dynamic OCD settings are a game-changer for developers. Thanks for highlighting this use case."
    cantilever rack jaipur
    slotted angle rack mumbai

    ReplyDelete
  12. "I appreciate the detailed explanation. Could you add a section on common pitfalls and how to avoid them?"
    Rack Manufacturer in India
    cantilever rack

    ReplyDelete
  13. "Very well written! The example with Sling ResourceChangeListener made the concept clear."
    mezzanine floor
    mobile compactor in noida

    ReplyDelete
  14. "This is exactly what I needed for my project. Thank you for such a clear and concise guide!"
    industrial rack In gurgaon
    industrial storage rack in ghaziabad

    ReplyDelete
  15. "Fantastic post! How does this impact performance in large-scale applications?"
    Centrifugal Blowers
    Distributorship Opportunities

    ReplyDelete
  16. "I learned something new today. Thank you for making dynamic OSGi properties so accessible."
    Pickle Distributorship
    Ice Cream Parlour Franchise

    ReplyDelete

Post a Comment

Popular posts from this blog

Embedding Third party dependency/OSGi bundle in AEM application hosted in AEMasCS

OSGI Factory Configuration implementation

Embed Third party dependency using bnd-maven-plugin