java - programada - Programador de tareas, @ Programado y cuarzo.
expresiones cron (2)
Parece que no hay una implementación lista. Sin embargo, el cableado propio no debería ser muy difícil:
@Service
public class QuartzTaskScheduler implements TaskScheduler {
//...
}
Y haciendo primavera para usarlo:
<task:annotation-driven/>
<bean class="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor">
<property name="scheduler" ref="quartzTaskScheduler"/>
</bean>
Si sigue esta ruta, considere la posibilidad de aportar su código a Spring Framework (paquete org.springframework.scheduling.quartz
) o al menos abra un problema para eso.
¿Hay alguna forma de tener @Scheduled
con cuarzo como el planificador subyacente?
Hay dos cosas en las que puedo pensar, pero ambas requieren algo de trabajo:
- cree un
BeanPostProcessor
personalizado que analizará la anotación@Scheduled
y registrará los trabajos de cuarzo - implementar
TaskScheduler
para delegar en elScheduler
cuarzo.
La pregunta es: ¿hay algo ya escrito para las dos opciones anteriores y hay otra opción?
Terminé haciendo mi propio "puente" de cuarzo de primavera. Planeo sugerirlo como mejora para la primavera.
Primero, creé una nueva anotación, que se colocará en las clases que implementan la interfaz de trabajo de cuarzo:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
@Scope("prototype")
public @interface ScheduledJob {
String cronExpression() default "";
long fixedRate() default -1;
boolean durable() default false;
boolean shouldRecover() default true;
String name() default "";
String group() default "";
}
(Tenga en cuenta que el alcance del prototipo - cuarzo asume que cada ejecución de trabajo es una nueva instancia. No soy un experto en cuarzo, así que me conformé con esa expectativa. Si resulta redundante, simplemente puede eliminar la anotación de @Scope)
Luego definí un ApplicationListener que, cada vez que el contexto se actualiza (o comienza) busca todas las clases anotadas con @ScheduledJob y las registra en el programador de cuarzo:
/**
* This class listeners to ContextStartedEvent, and when the context is started
* gets all bean definitions, looks for the @ScheduledJob annotation,
* and registers quartz jobs based on that.
*
* Note that a new instance of the quartz job class is created on each execution,
* so the bean has to be of "prototype" scope. Therefore an applicationListener is used
* rather than a bean postprocessor (unlike singleton beans, prototype beans don''t get
* created on application startup)
*
* @author bozho
*
*/
public class QuartzScheduledJobRegistrar implements
EmbeddedValueResolverAware, ApplicationContextAware,
ApplicationListener<ContextRefreshedEvent> {
private Scheduler scheduler;
private StringValueResolver embeddedValueResolver;
private Map<JobListener, String> jobListeners;
private ApplicationContext applicationContext;
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@SuppressWarnings("unchecked")
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext() == this.applicationContext) {
try {
scheduler.clear();
for (Map.Entry<JobListener, String> entry : jobListeners.entrySet()) {
scheduler.getListenerManager().addJobListener(entry.getKey(), NameMatcher.nameStartsWith(entry.getValue()));
}
} catch (SchedulerException ex) {
throw new IllegalStateException(ex);
}
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
String[] definitionNames = factory.getBeanDefinitionNames();
for (String definitionName : definitionNames) {
BeanDefinition definition = factory.getBeanDefinition(definitionName);
try {
if (definition.getBeanClassName() != null) {
Class<?> beanClass = Class.forName(definition.getBeanClassName());
registerJob(beanClass);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}
}
public void registerJob(Class<?> targetClass) {
ScheduledJob annotation = targetClass.getAnnotation(ScheduledJob.class);
if (annotation != null) {
Assert.isTrue(Job.class.isAssignableFrom(targetClass),
"Only classes implementing the quartz Job interface can be annotated with @ScheduledJob");
@SuppressWarnings("unchecked") // checked on the previous line
Class<? extends Job> jobClass = (Class<? extends Job>) targetClass;
JobDetail jobDetail = JobBuilder.newJob()
.ofType(jobClass)
.withIdentity(
annotation.name().isEmpty() ? targetClass.getSimpleName() : annotation.name(),
annotation.group().isEmpty() ? targetClass.getPackage().getName() : annotation.group())
.storeDurably(annotation.durable())
.requestRecovery(annotation.shouldRecover())
.build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger()
.withIdentity(jobDetail.getKey().getName() + "_trigger", jobDetail.getKey().getGroup() + "_triggers")
.startNow();
String cronExpression = annotation.cronExpression();
long fixedRate = annotation.fixedRate();
if (!BooleanUtils.xor(new boolean[] {!cronExpression.isEmpty(), fixedRate >=0})) {
throw new IllegalStateException("Exactly one of ''cronExpression'', ''fixedRate'' is required. Offending class " + targetClass.getName());
}
if (!cronExpression.isEmpty()) {
if (embeddedValueResolver != null) {
cronExpression = embeddedValueResolver.resolveStringValue(cronExpression);
}
try {
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
if (fixedRate >= 0) {
triggerBuilder.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(fixedRate)
.repeatForever())
.withIdentity(jobDetail.getKey().getName() + "_trigger", jobDetail.getKey().getGroup() + "_triggers");
}
try {
scheduler.scheduleJob(jobDetail, triggerBuilder.build());
} catch (SchedulerException e) {
throw new IllegalStateException(e);
}
}
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void setJobListeners(Map<JobListener, String> jobListeners) {
this.jobListeners = jobListeners;
}
}
Luego necesitaba un JobFactory personalizado para enchufar el cuarzo para que los trabajos sean creados por el contexto de Spring:
public class QuartzSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private SchedulerContext schedulerContext;
private ApplicationContext ctx;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Job job = ctx.getBean(bundle.getJobDetail().getJobClass());
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
if (this.schedulerContext != null) {
pvs.addPropertyValues(this.schedulerContext);
}
bw.setPropertyValues(pvs, true);
return job;
}
public void setSchedulerContext(SchedulerContext schedulerContext) {
this.schedulerContext = schedulerContext;
super.setSchedulerContext(schedulerContext);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.ctx = applicationContext;
}
}
Finalmente, la configuración xml:
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="com.foo.bar.scheduling.QuartzSpringBeanJobFactory" />
</property>
</bean>
<bean id="scheduledJobRegistrar" class="com.foo.bar.scheduling.QuartzScheduledJobRegistrar">
<property name="scheduler" ref="quartzScheduler" />
<property name="jobListeners">
<map>
<entry value=""> <!-- empty string = match all jobs -->
<key><bean class="com.foo.bar.scheduling.FailuresJobListener"/></key>
</entry>
</map>
</property>
</bean>