Skip to content

Commit

Permalink
Merge pull request #51 from mqttsnet/dev
Browse files Browse the repository at this point in the history
add DefinitionRegistrar
  • Loading branch information
xiaonannet authored Sep 15, 2023
2 parents a700a9b + 1b3fdf1 commit a04af2e
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mqttsnet.thinglinks.link.common.config;

import com.mqttsnet.thinglinks.common.core.mqs.SelectorConfig;
import com.mqttsnet.thinglinks.link.common.consumer.kafka.DeviceActionMessageKafkaConsumer;
import com.mqttsnet.thinglinks.link.common.consumer.rocketmq.DeviceActionMessageRocketmqConsumer;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

@Configuration
class ThingLinksConfiguration {

@Bean
public ThingLinksBeanDefinitionRegistrar thingLinksBeanDefinitionRegistrar(Environment environment) {
return new ThingLinksBeanDefinitionRegistrar(environment);
}
}

public class ThingLinksBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

private final Environment environment;

public ThingLinksBeanDefinitionRegistrar(Environment environment) {
this.environment = environment;
}

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Class<?> beanClass = getConsumerClass();
String beanName = beanClass.getSimpleName();
registerBeanDefinition(registry, beanClass, beanName);
}

private Class<?> getConsumerClass() {
boolean selectorKafka = Boolean.parseBoolean(environment.getProperty(SelectorConfig.selectorKafkaKey, "false"));
return selectorKafka ? DeviceActionMessageKafkaConsumer.class :
DeviceActionMessageRocketmqConsumer.class;
}

private void registerBeanDefinition(BeanDefinitionRegistry registry, Class<?> beanClass, String beanName) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(beanClass).getBeanDefinition();
registry.registerBeanDefinition(beanName, beanDefinition);
}
}

0 comments on commit a04af2e

Please sign in to comment.