net.leanix.dropkit.quartz.SimpleManagedJobScheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leanix-dropkit Show documentation
Show all versions of leanix-dropkit Show documentation
Base functionality for leanIX dropwizard-based services
package net.leanix.dropkit.quartz;
import javax.inject.Inject;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Singleton;
import io.dropwizard.lifecycle.Managed;
/**
* This is a analog implemenation to com.fiestacabin.dropwizard.quartz.ManagedScheduler
used to setup time base running jobs.
* The difference (and advantage) to ManagedScheduler is here that the job class does not need the @Scheduled
annotation and
* can be configured by standard dropwizard configuration mechanism.
*
* Usage:
*
*
*
* // register SimpleManagedJobScheduler scheduler to start/stop with the jetty server
* environment.lifecycle().manage(injector.getInstance(SimpleManagedJobScheduler.class));
* // add job class
* injector.getInstance(SimpleManagedJobScheduler.class).addJob(MyJob.class,
* config.getJobsConfiguration().getIntervallMillis());
*
*
* @author ralfwehner
*
*/
@Singleton
public class SimpleManagedJobScheduler implements Managed {
private static final Logger LOG = LoggerFactory.getLogger(SimpleManagedJobScheduler.class);
private final Scheduler scheduler;
private final LxGuiceJobFactory jobFactory;
@Inject
public SimpleManagedJobScheduler(Scheduler scheduler, LxGuiceJobFactory jobFactory) throws SchedulerException {
this.scheduler = new StdSchedulerFactory().getScheduler();
this.jobFactory = jobFactory;
}
public static SimpleManagedJobScheduler getInstance() throws SchedulerException {
return new SimpleManagedJobScheduler(new StdSchedulerFactory().getScheduler(), null);
}
/**
* Add a job that is executed each n milliseconds.
*
* @param jobClass
* @param intervalInMilliseconds
* @throws SchedulerException
*/
public void addJob(Class extends Job> jobClass, long intervalInMilliseconds)
throws SchedulerException {
if (intervalInMilliseconds <= 0) {
throw new IllegalArgumentException("Parameter intervalInMilliseconds must be greater than 0.");
}
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(intervalInMilliseconds).repeatForever())
.build();
LOG.info("Adding new job {} with trigger {} to quartz scheduler.", jobDetail, trigger);
scheduler.scheduleJob(jobDetail, trigger);
}
public Scheduler getScheduler() {
return scheduler;
}
@Override
public void start() throws Exception {
LOG.info("Starting quartz scheduler");
if (jobFactory != null) {
scheduler.setJobFactory(jobFactory);
}
scheduler.start();
}
@Override
public void stop() throws Exception {
LOG.info("Stopping quartz scheduler");
scheduler.shutdown(true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy