Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Extending OSGI Configuration | Different configurations of a service according to the time of the server

Avatar

Level 2

CQ ConfigAdmin picks a folder based on the runmode like if runmode was author & dev, it will pick config.author.dev folder automatically. I have a usecase where-in my custom service is supposed to use two different set of properties based on whether its day or night. I know we could have an if-else in service itself and taking a superset of all the parameters, so wondering if runmode can help me here. Like if I create two different folders say config.author.dev.day & config.author.dev.night and then creating under each of it osgi:config node same as PID of my service. This will make it more flexible but have not found a way to do the same so far. Can you help?

1 Accepted Solution

Avatar

Correct answer by
Level 6

I think you are looking at the problem from a slightly wrong angle.

Your angle is: I need to have ONE service that can do A or B depending on the time.

My angle would be: I need a service that can create a task that runs depending on the date-time-pattern.

This means that I would create a service factory, and have the instances run depending on a time pattern. Take a look at the Workflow purger. The purger service does not do anything unless you create an instance of a purger task, that is run depending on the date-time-pattern.

 

/Ove

View solution in original post

7 Replies

Avatar

Level 2

Defining it via run mode will not help as once the server has started the runmode will never change, thus if you started your server in *.day mode it will stay fixed still the time server is running and run mode configurations are not updated (requires restart).

You can look at a refreshable/reload-able configuration based implementation and have a scheduler service that updates you config based on your time based requirements. You will have to do decent amount of coding for building such reload-able configuration.

Will update in  case I can find out a better approach of achieving this requirement.

Thanks
Ameesh

Avatar

Level 2

Thanks Ameesh. I know CQ ootb runmode works that way and it is fixed. I was more looking for hooks/extensions to change it at run time. I would assume folders are to be picked by some apis and was hoping to use that or extend it.

 

Also I don't want configuration to be changed by another code because it will be too risky since all these configuration defines my system behavior. Please let me know if you want more information.

Avatar

Level 2

Changing runmodes is anyways not a right approach to take. If you could describe in details the use-case you are trying to work on it would be helpful in exploring an approach.

Thanks
Ameesh

Avatar

Level 2

Use case is in my first post. I was not talking about changing runmodes above since this can not be done, what I meant was using the same api which picks the right folder based on current runmodes. I was wondering if there is a method which takes string array (runmodes) and give me the configuration based on a PID. I am sure CQ must be using some sorts of API itself to load and cache the configurations.

Avatar

Correct answer by
Level 6

I think you are looking at the problem from a slightly wrong angle.

Your angle is: I need to have ONE service that can do A or B depending on the time.

My angle would be: I need a service that can create a task that runs depending on the date-time-pattern.

This means that I would create a service factory, and have the instances run depending on a time pattern. Take a look at the Workflow purger. The purger service does not do anything unless you create an instance of a purger task, that is run depending on the date-time-pattern.

 

/Ove

Avatar

Level 2

Thanks Ove. That makes sense if configuration is same. My use case is slightly different which is without making my service aware of configurations, I want it to pick two different set of configurations. In your solutions then service factory should first figure out and load the correct configurations and then pass it to the service. If I have more services I will end up having service factory for all. And also the responsibility which I am giving to servicefactory class around configurations should not be with it. Sling solves it out of the box (using runmodes) so was thinking to leverage that because this solution looks much cleaner.

Avatar

Level 10

IMO no such hooks available. Configure your service to have both day & night settings. In service factory have the instances pick right configuration based on a time pattern.