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);
}
}
}
Aggregations