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

New Participant
February 9, 2018

@Component(enabled = true, immediate = true, metatype = true, label = "cron job",
   description = "cron job ")

@Service(value = Runnable.class)

@Properties({

   @Property(name = "scheduler.expression", value = "0 0/15 * * * ?", description = "Cron-job expression Ex: '0 0/15 * * * ?' for 15 Mins"),
   @Property(name = "scheduler.concurrent", boolValue = false)

})

@Slf4j
public class TestScheduler implements Runnable {

...

}

smacdonald2008
New Participant
February 9, 2018

WHen setting up a schedule - are you using the org.apache.sling.commons.scheduler.Scheduler API?

New Participant
February 9, 2018

I have an actual scheduler but it runs not on startup only after the 4 hours of waiting and I want it the be run immediately.

What's that: tireDesignsFetchJobFactory.createJob()

New Participant
February 9, 2018

Let me try to see if I understand correctly what you're trying to achieve here.

You have application code (an OSGi component) that schedules a specific task, every 4 hours. You want this task to be done after you've deployed your application code aswell. Let's take the following code as your OSGi component as an example:

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 = CustomSchedulerServiceImpl.class, configurationPolicy = ConfigurationPolicy.REQUIRE, immediate = true)

public class CustomSchedulerServiceImpl implements CustomSchedulerServiceImpl {

   /**
  * Cron formatted scheduling expression
  */
   private String schedulerExpression;

   @Reference
   private Scheduler scheduler;

   /**
  * Activate.
  *
  * @param config the polling service config
  */
   @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(tireDesignsFetchJobFactory.createJob(), scheduleOptions);
   }

  @Override
  public void scheduleINow() {

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

}

New Participant
February 9, 2018

Can you point me to an example?

New Participant
February 9, 2018

Maybe you can schedule it again when activating the component?

@Activate or @Modified