Restricting Scheduler to run in published Env. | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
khamat_bn
khamat_bnAccepted solution
Level 4
Oct 12, 2019 11:58 AM UTC

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...

}

4 replies

joerghoh
Employee
Oct 13, 2019 10:23 AM UTC

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.

Translate
khamat_bn
khamat_bnAccepted solution
Level 4
Oct 12, 2019 11:58 AM UTC

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...

}

Translate
ArpitVarshney
New Participant
Oct 12, 2019 7:07 AM UTC

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

Translate
Ankur_Khare
New Participant
Oct 12, 2019 6:59 AM UTC

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.

Translate