org.nnsoft.guice.guartz.QuartzModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guartz Show documentation
Show all versions of guartz Show documentation
Guartz is the Google Guice extension for Quartz Scheduler,
originally developed by Nino Martinez Wael
/*
* Copyright 2009-2011 The 99 Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nnsoft.guice.guartz;
import static com.google.inject.Scopes.SINGLETON;
import static com.google.inject.internal.util.$Preconditions.checkNotNull;
import static com.google.inject.internal.util.$Preconditions.checkState;
import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static java.util.TimeZone.getTimeZone;
import static org.nnsoft.guice.guartz.Scheduled.DEFAULT;
import java.util.TimeZone;
import org.quartz.Job;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerListener;
import org.quartz.TriggerListener;
import org.quartz.spi.JobFactory;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
/**
* Quartz (http://www.quartz-scheduler.org/) Module as Google-Guice extension.
*/
public abstract class QuartzModule extends AbstractModule {
private Multibinder jobListeners;
private Multibinder triggerListeners;
private Multibinder schedulerListeners;
/**
* {@inheritDoc}
*/
@Override
protected final void configure() {
checkState(jobListeners == null, "Re-entry is not allowed.");
checkState(triggerListeners == null, "Re-entry is not allowed.");
checkState(schedulerListeners == null, "Re-entry is not allowed.");
jobListeners = newSetBinder(binder(), JobListener.class);
triggerListeners = newSetBinder(binder(), TriggerListener.class);
schedulerListeners = newSetBinder(binder(), SchedulerListener.class);
try {
schedule();
bind(JobFactory.class).to(InjectorJobFactory.class).in(SINGLETON);
bind(Scheduler.class).toProvider(SchedulerProvider.class).asEagerSingleton();
} finally {
jobListeners = null;
triggerListeners = null;
schedulerListeners = null;
}
}
/**
* Part of the EDSL builder language for configuring {@code Job}s.
* Here is a typical example of scheduling {@code Job}s when creating your Guice injector:
*
*
* Guice.createInjector(..., new QuartzModule() {
*
* {@literal @}Override
* protected void schedule() {
* scheduleJob(MyJobImpl.class).withCronExpression("0/2 * * * * ?");
* }
*
* });
*
*
* @see JobSchedulerBuilder
*/
protected abstract void schedule();
/**
* Add the {@code JobListener} binding.
*
* @param jobListenerType The {@code JobListener} class has to be bound
*/
protected final void addJobListener(Class extends JobListener> jobListenerType) {
doBind(jobListeners, jobListenerType);
}
/**
* Add the {@code TriggerListener} binding.
*
* @param triggerListenerType The {@code TriggerListener} class has to be bound
*/
protected final void addTriggerListener(Class extends TriggerListener> triggerListenerType) {
doBind(triggerListeners, triggerListenerType);
}
/**
* Add the {@code SchedulerListener} binding.
*
* @param schedulerListenerType The {@code SchedulerListener} class has to be bound
*/
protected final void addSchedulerListener(Class extends SchedulerListener> schedulerListenerType) {
doBind(schedulerListeners, schedulerListenerType);
}
/**
* Allows {@code Job} scheduling, delegating Guice create the {@code Job} instance
* and inject members.
*
* If given {@code Job} class is annotated with {@link Scheduled}, then {@code Job}
* and related {@code Trigger} values will be extracted from it.
*
* @param jobClass The {@code Job} has to be scheduled
* @return The {@code Job} builder
*/
protected final JobSchedulerBuilder scheduleJob(Class extends Job> jobClass) {
checkNotNull(jobClass, "Argument 'jobClass' must be not null.");
JobSchedulerBuilder builder = new JobSchedulerBuilder(jobClass);
if (jobClass.isAnnotationPresent(Scheduled.class)) {
Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
builder
// job
.withJobName(scheduled.jobName())
.withJobGroup(scheduled.jobGroup())
.withRequestRecovery(scheduled.requestRecovery())
.withStoreDurably(scheduled.storeDurably())
// trigger
.withCronExpression(scheduled.cronExpression())
.withTriggerName(scheduled.triggerName());
if (!DEFAULT.equals(scheduled.timeZoneId())) {
TimeZone timeZone = getTimeZone(scheduled.timeZoneId());
if (timeZone != null) {
builder.withTimeZone(timeZone);
}
}
}
requestInjection(builder);
return builder;
}
/**
* Utility method to respect the DRY principle.
*
* @param
* @param binder
* @param type
*/
protected final void doBind(Multibinder binder, Class extends T> type) {
checkNotNull(type);
binder.addBinding().to(type);
}
}