Search in sources :

Example 1 with MessageHandler

use of com.jeesuite.kafka.handler.MessageHandler in project jeesuite-libs by vakinge.

the class TopicConsumerSpringProvider method scanAndRegisterAnnotationTopics.

private void scanAndRegisterAnnotationTopics(String[] scanBasePackages) {
    String RESOURCE_PATTERN = "/**/*.class";
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    for (String scanBasePackage : scanBasePackages) {
        logger.info(">>begin scan package [{}] with Annotation[ConsumerHandler] MessageHanlder ", 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(ConsumerHandler.class)) {
                        ConsumerHandler annotation = clazz.getAnnotation(ConsumerHandler.class);
                        MessageHandler hander = (MessageHandler) context.getBean(clazz);
                        if (!topicHandlers.containsKey(annotation.topic())) {
                            topicHandlers.put(annotation.topic(), hander);
                            logger.info("register new MessageHandler:{}-{}", annotation.topic(), clazz.getName());
                        }
                    }
                }
            }
            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) RetryErrorMessageHandler(com.jeesuite.kafka.consumer.hanlder.RetryErrorMessageHandler) MessageHandler(com.jeesuite.kafka.handler.MessageHandler) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) BeansException(org.springframework.beans.BeansException) ConsumerHandler(com.jeesuite.kafka.annotation.ConsumerHandler) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

ConsumerHandler (com.jeesuite.kafka.annotation.ConsumerHandler)1 RetryErrorMessageHandler (com.jeesuite.kafka.consumer.hanlder.RetryErrorMessageHandler)1 MessageHandler (com.jeesuite.kafka.handler.MessageHandler)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