com.fenglinga.tinyspring.scheduling.ScheduledTaskRegistrar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinyspring Show documentation
Show all versions of tinyspring Show documentation
A simple and flexible web service framework based on Mina
package com.fenglinga.tinyspring.scheduling;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import com.fenglinga.tinyspring.common.Utils;
import com.fenglinga.tinyspring.framework.annotation.Component;
import com.fenglinga.tinyspring.scheduling.annotation.Scheduled;
public class ScheduledTaskRegistrar {
private List cronTasks = new ArrayList();
private ScheduledThreadPoolExecutor taskScheduler = null;
public ScheduledTaskRegistrar(ScheduledThreadPoolExecutor executor) {
this.taskScheduler = executor;
try {
Set> klassSet = Utils.getClasses("task");
for (Class> klass : klassSet) {
boolean isComponent = false;
Annotation[] annos = klass.getAnnotations();
for (Annotation annotation : annos) {
Class> annotationType = annotation.annotationType();
if (annotationType.getSimpleName().equals(Component.class.getSimpleName())) {
isComponent = true;
break;
}
}
if (!isComponent) {
continue;
}
Method[] methods = klass.getDeclaredMethods();
Object bean = klass.newInstance();
for (Method method : methods) {
Scheduled schd = method.getAnnotation(Scheduled.class);
if (schd == null) {
continue;
}
processScheduled(schd, method, bean);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void processScheduled(Scheduled scheduled, Method method, Object bean) {
Runnable runnable = new ScheduledMethodRunnable(bean, method);
CronTask cronTask = new CronTask(runnable, new CronTrigger(scheduled.cron()));
new ReschedulingRunnable(cronTask.getRunnable(), cronTask.getTrigger(), taskScheduler).schedule();
cronTasks.add(cronTask);
}
}