use of com.jeesuite.scheduler.annotation.ScheduleConf in project jeesuite-libs by vakinge.
the class SchedulerFactoryBeanWrapper method scanAndRegisterAnnotationJobs.
private void scanAndRegisterAnnotationJobs(String[] scanBasePackages) {
String RESOURCE_PATTERN = "/**/*.class";
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
for (String scanBasePackage : scanBasePackages) {
logger.info(">>begin scan package [{}] with Annotation[ScheduleConf] jobs ", scanBasePackage);
try {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(scanBasePackage) + RESOURCE_PATTERN;
org.springframework.core.io.Resource[] resources = resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
for (org.springframework.core.io.Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
Class<?> clazz = Class.forName(className);
if (clazz.isAnnotationPresent(ScheduleConf.class)) {
ScheduleConf annotation = clazz.getAnnotation(ScheduleConf.class);
AbstractJob job = (AbstractJob) context.getBean(clazz);
job.setCronExpr(annotation.cronExpr());
job.setExecuteOnStarted(annotation.executeOnStarted());
job.setGroup(groupName);
job.setJobName(annotation.jobName());
job.setRetries(annotation.retries());
if (!schedulers.contains(job)) {
schedulers.add(job);
logger.info("register new job:{}", ToStringBuilder.reflectionToString(job, ToStringStyle.JSON_STYLE));
} else {
logger.info("Job[{}] is registered", job.getJobName());
}
}
}
}
logger.info("<<scan package[" + scanBasePackage + "] finished!");
} catch (Exception e) {
if (e instanceof org.springframework.beans.factory.NoSuchBeanDefinitionException) {
throw (org.springframework.beans.factory.NoSuchBeanDefinitionException) e;
}
logger.error("<<scan package[" + scanBasePackage + "] error", e);
}
}
}
Aggregations