Restricting Scheduler to run in published Env.

Avatar

Avatar

venusj64090183

Avatar

venusj64090183

venusj64090183

11-10-2019

I want to restrict the scheduler to run in published env. which approach will be good for this?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

khamat_bn

Avatar

khamat_bn

khamat_bn

12-10-2019

In the run() of scheduler you have to runmode wheather its autour or publisher

@Reference

    private SlingSettingsService settingsService;

    private Set<String> runModes;

    @Override

    public void run() {

        this.runModes = settingsService.getRunModes();

        if (null != runModes && runModes.contains("author")) {

//your code goes here...

}

Answers (3)

Answers (3)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K
Jörg_Hoh
Employee

13-10-2019

While the answer by khamat.bn​ works for that, the best solution for this is the one by arpitv27529355​: You should never hardcode this in the code, but rely on configuration instead.

Personally I would consider every call of SlingSettingsService.getRunmodes() as bad practice.

Avatar

Avatar

ArpitVarshney

MVP

Avatar

ArpitVarshney

MVP

ArpitVarshney
MVP

12-10-2019

Hi venusj64090183

In your service, you can define policy = ConfigurationPolicy.REQUIRE as below

@Component(policy = ConfigurationPolicy.REQUIRE) 

Now create OSGi config only for publish run mode. Your service will not satisfied with the author instance.

Reference: OSGi component in AEM that is active only in specific run mode (say, publish). – Compute Patterns

Regards,

Arpit

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

11-10-2019

Use run mode configuration.

Add a config in config.author folder so that scheduler will not run in author

Add a config in config.publish folder so that it gets executed in publish.

Hope this helps.