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