Trigger scheduler once after deployment | Community
Skip to main content
New Participant
February 9, 2018
Solved

Trigger scheduler once after deployment

  • February 9, 2018
  • 16 replies
  • 8246 views

Hi guys,

is it possible to trigger a scheduler once after every deployment? So the regular schedule time is 4 hours, but it will took 4 hours for the first run.

Any property to set?

Br,

Tim

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Hi,

I see. The only chance to get it working the way you want is to trigger the scheduled job manually on activation of a SCR component/service.

Jörg

16 replies

joerghoh
Employee
February 14, 2018

I guess so. But please validate, that the case of some activations/deactivations of the component in a short time is handled properly :-) Depending on your deployment processes this might happen.

Jörg

New Participant
February 14, 2018

So would it come down to the example I provided? (More or less)

joerghoh
Employee
February 13, 2018

I don't think that you need to do this. You can use the Scheduler API to immediately fire an event. But yes, you need to trigger that in an activate method.

Jörg

New Participant
February 11, 2018

Yes that what i thought, but then it will block all other components on startup because its maybe running for 30 minutes. So no chance to do it this way. I have to create a new Service and trigger it by a custom scheduler.

Thanks for all replies.

joerghoh
joerghohAccepted solution
Employee
February 10, 2018

Hi,

I see. The only chance to get it working the way you want is to trigger the scheduled job manually on activation of a SCR component/service.

Jörg

smacdonald2008
New Participant
February 10, 2018

This is an interesting use case - when you setup a scheduler to fire every 4 hours - the first time it will count 4 hours.  See if you can modify the code to fire once 1 when its activated.

New Participant
February 10, 2018

If want to achieve that the scheduler runs directly on statup/after deployment but it seems it just starting after 4 hours for the first time and after that every 4 hours.

The problem is the scheduler pulls data into AEM and otherwise I have to wait 4 hours for the very first execution.

So the component shows nothing for the first 4 hours.

joerghoh
Employee
February 9, 2018

What do you want to achieve? Can we rephrase your requirement to something like this: "By default the scheduling interval should be 4 hours. In case it is taking more time, the process should not run in parallel, but serialized"?

In that case the section "preventing concurrent execution" of Apache Sling :: Scheduler Service (commons scheduler)  is covering that.

Jörg

New Participant
February 9, 2018

My bad

This was code that I've written for a project of mine and I wanted to remove anything that didn't have anything to do with your question. Seems like I missed something!

Anyway, that "tireDesignFactory" simply creates a new instance that implements Runnable, like your TestScheduler. (So it's custom code)

Your TestScheduler is not a scheduler, but a job itself. If you would use my example and create a factory that returns a new Runnable instance, then your scheduler service will schedule the job at a specific time, and you can also ask it to schedule a job right after you instantiate your bundle with the @Activate annotated method.

You would get something like this:

import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;

@Component(service = CustomSchedulerService.class, configurationPolicy = ConfigurationPolicy.REQUIRE, immediate = true)

public class CustomSchedulerServiceImpl implements CustomSchedulerService {

   private String schedulerExpression;

   @Reference
   private Scheduler scheduler;

   @Reference
   private CustomJobFactory factory;


   @Activate
   public void activate(final Map<String, Object> config) {

        this.schedulerExpression = <YOUR CRON EXPRESSION HERE PREFERABLY FROM CONFIG>

          scheduleNow();

         scheduleLater();

   }

   @Override
   public void scheduleLater() {

   final ScheduleOptions scheduleOptions = scheduler.EXPR(schedulerExpression);
    scheduler.schedule(factory.createJob(), scheduleOptions);
   }

  @Override
  public void scheduleINow() {

    final ScheduleOptions scheduleOptions = scheduler.NOW();
    scheduler.schedule(factory.createJob(), scheduleOptions);
  }

}

------------------

import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;

@Component(service = CustomJobFactory.class, immediate = true)

public class CustomJobFactoryImpl implements CustomJobFactory {

//you can use this class to get references from the OSGi container and pass those to your CustomJob instances

   @Override
   public void createJob() {

        return new CustomJob();
   }

}

--------------------

import org.apache.sling.commons.scheduler.Job;
import org.apache.sling.commons.scheduler.JobContext;

public class CustomJob implements Job {

   public CustomJob() {

 
   }

   @Override
   public void execute(final JobContext jobContext) {

   //do something
   }

}

VeenaVikraman
New Participant
February 9, 2018

I am not sure if I understood it correctly, but will an expression like below help ?

0 0 */4 ? * * - It will run every 4 hours starting next closest hour .. Unfortunately i could find one which start immediately