org.zodiac.autoconfigure.rabbit.RabbitMQAnnotationDrivenConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.rabbit;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.constants.AppConstants;
import org.zodiac.rabbit.constants.RabbitConstants;
import org.zodiac.rabbit.listener.ErrorTraceSimpleMessageListenerContainer;
@SpringBootConfiguration
@ConditionalOnClass(value = {org.springframework.amqp.rabbit.annotation.EnableRabbit.class})
class RabbitMQAnnotationDrivenConfiguration {
private final ObjectProvider messageConverter;
private final ObjectProvider messageRecoverer;
private final RabbitConfigProperties properties;
RabbitMQAnnotationDrivenConfiguration(ObjectProvider messageConverter,
ObjectProvider messageRecoverer, RabbitConfigProperties properties) {
this.messageConverter = messageConverter;
this.messageRecoverer = messageRecoverer;
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(name = RabbitConstants.LISTENER_CONTAINER_FACTORY_BEAN_NAME)
protected SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory,
Environment environment) {
EnhancedSimpleRabbitListenerContainerFactory factory = new EnhancedSimpleRabbitListenerContainerFactory(environment);
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean
@ConditionalOnMissingBean
protected SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() {
SimpleRabbitListenerContainerFactoryConfigurer configurer =
new SimpleRabbitListenerContainerFactoryConfigurer();
configurer.setMessageConverter(this.messageConverter.getIfUnique());
configurer.setMessageRecoverer(this.messageRecoverer.getIfUnique());
configurer.setRabbitInfo(this.properties);
return configurer;
}
@org.springframework.amqp.rabbit.annotation.EnableRabbit
@ConditionalOnMissingBean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
protected static class EnableRabbitConfiguration {
}
protected static class EnhancedSimpleRabbitListenerContainerFactory extends SimpleRabbitListenerContainerFactory {
private Environment environment;
public EnhancedSimpleRabbitListenerContainerFactory(Environment environment) {
this.environment = environment;
}
@Override
protected SimpleMessageListenerContainer createContainerInstance() {
return new ErrorTraceSimpleMessageListenerContainer(environment.getProperty(SystemPropertiesConstants.Spring.SPRING_APP_NAME, AppConstants.UNKNOWN));
}
}
}