Search in sources :

Example 1 with ScheduleConf

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);
        }
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) BeansException(org.springframework.beans.BeansException) ScheduleConf(com.jeesuite.scheduler.annotation.ScheduleConf) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

ScheduleConf (com.jeesuite.scheduler.annotation.ScheduleConf)1 BeansException (org.springframework.beans.BeansException)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)1 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)1 MetadataReader (org.springframework.core.type.classreading.MetadataReader)1 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)1