April 2, 2020
Estimated Post Reading Time ~

Sample runmode-based servlet/component/service restriction

QuestionAssume you have a couple of OSGi services/servlets within a bundle, among that one of the components required to run in author instance only. What is the best approach to deal with such kind of requirement?

Note: I have seen some of the implementation checking for specific run modes in the component & it is not a good idea. Also seen some implementation disabled the auto-start option by "@scr.component enabled="false" & then doing the manual task to start the service on the author instance.

SolutionTake advantage of DS’s built-in integration with the Configuration Admin service. Make the component/servlet require a configuration (policy=ConfigurationPolicy.REQUIRE) and Then create a run mode dependent configuration for that component.

ConfigurationPolicy REQUIRE implementation exampleStep1: For the component that requires a restriction to run in specific run mode define using OSGi Declarative Services @Component(policy=ConfigurationPolicy.REQUIRE)
 


Step2: Build a bundle. In Felix console, you will notice the component is not created and will be in the unsatisfied state.

Step3: In the above example the component blog.sample.MyComponent will only be created if a configuration records with the PID blog.sample.MyComponent exists. Assume you need this component for runmode author only then create the configuration in respective runmode.


Step4: In the author's instance you will notice component will be created and in a satisfying state.


Click Here to Download this sample bundle


By aem4beginner

No comments:

Post a Comment

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