All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.rabbitmq.RabbitMQConfig Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.rabbitmq;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.infusers.core.rabbitmq.RabbitMQProperties.QueueConfig;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    @Autowired
    private RabbitMQProperties properties;

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public Map queues() {
        return properties.getQueues().stream()
            .collect(Collectors.toMap(
                QueueConfig::getName,
                queueConfig -> {
                    QueueBuilder builder = QueueBuilder.durable(queueConfig.getName());
                    
                    // Set TTL if provided
                    if (queueConfig.getTtl() != null) {
                        builder.ttl(queueConfig.getTtl());
                    }
                    
                    // Set Dead Letter Exchange if provided
                    if (queueConfig.getDeadLetterExchange() != null) {
                        builder.withArgument("x-dead-letter-exchange", queueConfig.getDeadLetterExchange());
                    }
                    
                    return builder.build();
                }
            ));
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(properties.getExchange().getName());
    }

    @Bean
    public List bindings() {
        return properties.getQueues().stream()
            .map(queueConfig -> {
                if (queueConfig.getRoutingKey() == null) {
                    throw new IllegalArgumentException("Routing key must be provided for queue: " + queueConfig.getName());
                }
                return BindingBuilder
                    .bind(queues().get(queueConfig.getName()))
                    .to(exchange())
                    .with(queueConfig.getRoutingKey());
            })
            .collect(Collectors.toList());
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setFailedDeclarationRetryInterval(10 * 60 * 1000L);
        return factory;
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Object initializeRabbitMQ() {
        declareQueuesAndBindings();
        return new Object(); // Dummy return to satisfy the @Bean method requirement
    }

    private void declareQueuesAndBindings() {
        RabbitAdmin admin = rabbitAdmin();
        queues().values().forEach(admin::declareQueue);
        admin.declareExchange(exchange());
        bindings().forEach(admin::declareBinding);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy