RabbitMQConfig.java 6.93 KB
package com.ecommerce.notification.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    
    // Exchange names
    public static final String NOTIFICATION_EXCHANGE = "notification.exchange";
    public static final String ORDER_EXCHANGE = "order.exchange";
    public static final String PAYMENT_EXCHANGE = "payment.exchange";
    public static final String INVENTORY_EXCHANGE = "inventory.exchange";
    
    // Queue names for notification service
    public static final String ORDER_CREATED_QUEUE = "notification.order.created.queue";
    public static final String ORDER_STATUS_QUEUE = "notification.order.status.queue";
    public static final String ORDER_CANCELLED_QUEUE = "notification.order.cancelled.queue";
    public static final String PAYMENT_SUCCESS_QUEUE = "notification.payment.success.queue";
    public static final String PAYMENT_FAILED_QUEUE = "notification.payment.failed.queue";
    public static final String PAYMENT_REFUND_QUEUE = "notification.payment.refund.queue";
    public static final String INVENTORY_LOW_STOCK_QUEUE = "notification.inventory.low.stock.queue";
    public static final String INVENTORY_OUT_OF_STOCK_QUEUE = "notification.inventory.out.of.stock.queue";
    public static final String INVENTORY_RESTOCK_QUEUE = "notification.inventory.restock.queue";
    
    // Routing keys
    public static final String NOTIFICATION_SENT_ROUTING_KEY = "notification.sent";
    public static final String NOTIFICATION_FAILED_ROUTING_KEY = "notification.failed";
    public static final String ORDER_CREATED_ROUTING_KEY = "order.created";
    public static final String ORDER_STATUS_ROUTING_KEY = "order.status.updated";
    public static final String ORDER_CANCELLED_ROUTING_KEY = "order.cancelled";
    public static final String PAYMENT_SUCCESS_ROUTING_KEY = "payment.success";
    public static final String PAYMENT_FAILED_ROUTING_KEY = "payment.failed";
    public static final String PAYMENT_REFUND_ROUTING_KEY = "refund.processed";
    public static final String INVENTORY_LOW_STOCK_ROUTING_KEY = "inventory.low.stock";
    public static final String INVENTORY_OUT_OF_STOCK_ROUTING_KEY = "inventory.out.of.stock";
    public static final String INVENTORY_RESTOCK_ROUTING_KEY = "inventory.restock";
    
    @Bean
    public TopicExchange notificationExchange() {
        return new TopicExchange(NOTIFICATION_EXCHANGE);
    }
    
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(ORDER_EXCHANGE);
    }
    
    @Bean
    public TopicExchange paymentExchange() {
        return new TopicExchange(PAYMENT_EXCHANGE);
    }
    
    @Bean
    public TopicExchange inventoryExchange() {
        return new TopicExchange(INVENTORY_EXCHANGE);
    }
    
    @Bean
    public Queue orderCreatedQueue() {
        return new Queue(ORDER_CREATED_QUEUE, true);
    }
    
    @Bean
    public Queue orderStatusQueue() {
        return new Queue(ORDER_STATUS_QUEUE, true);
    }
    
    @Bean
    public Queue orderCancelledQueue() {
        return new Queue(ORDER_CANCELLED_QUEUE, true);
    }
    
    @Bean
    public Queue paymentSuccessQueue() {
        return new Queue(PAYMENT_SUCCESS_QUEUE, true);
    }
    
    @Bean
    public Queue paymentFailedQueue() {
        return new Queue(PAYMENT_FAILED_QUEUE, true);
    }
    
    @Bean
    public Queue paymentRefundQueue() {
        return new Queue(PAYMENT_REFUND_QUEUE, true);
    }
    
    @Bean
    public Queue inventoryLowStockQueue() {
        return new Queue(INVENTORY_LOW_STOCK_QUEUE, true);
    }
    
    @Bean
    public Queue inventoryOutOfStockQueue() {
        return new Queue(INVENTORY_OUT_OF_STOCK_QUEUE, true);
    }
    
    @Bean
    public Queue inventoryRestockQueue() {
        return new Queue(INVENTORY_RESTOCK_QUEUE, true);
    }
    
    @Bean
    public Binding orderCreatedBinding(Queue orderCreatedQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderCreatedQueue)
                .to(orderExchange)
                .with(ORDER_CREATED_ROUTING_KEY);
    }
    
    @Bean
    public Binding orderStatusBinding(Queue orderStatusQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderStatusQueue)
                .to(orderExchange)
                .with(ORDER_STATUS_ROUTING_KEY);
    }
    
    @Bean
    public Binding orderCancelledBinding(Queue orderCancelledQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderCancelledQueue)
                .to(orderExchange)
                .with(ORDER_CANCELLED_ROUTING_KEY);
    }
    
    @Bean
    public Binding paymentSuccessBinding(Queue paymentSuccessQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(paymentSuccessQueue)
                .to(paymentExchange)
                .with(PAYMENT_SUCCESS_ROUTING_KEY);
    }
    
    @Bean
    public Binding paymentFailedBinding(Queue paymentFailedQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(paymentFailedQueue)
                .to(paymentExchange)
                .with(PAYMENT_FAILED_ROUTING_KEY);
    }
    
    @Bean
    public Binding paymentRefundBinding(Queue paymentRefundQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(paymentRefundQueue)
                .to(paymentExchange)
                .with(PAYMENT_REFUND_ROUTING_KEY);
    }
    
    @Bean
    public Binding inventoryLowStockBinding(Queue inventoryLowStockQueue, TopicExchange inventoryExchange) {
        return BindingBuilder.bind(inventoryLowStockQueue)
                .to(inventoryExchange)
                .with(INVENTORY_LOW_STOCK_ROUTING_KEY);
    }
    
    @Bean
    public Binding inventoryOutOfStockBinding(Queue inventoryOutOfStockQueue, TopicExchange inventoryExchange) {
        return BindingBuilder.bind(inventoryOutOfStockQueue)
                .to(inventoryExchange)
                .with(INVENTORY_OUT_OF_STOCK_ROUTING_KEY);
    }
    
    @Bean
    public Binding inventoryRestockBinding(Queue inventoryRestockQueue, TopicExchange inventoryExchange) {
        return BindingBuilder.bind(inventoryRestockQueue)
                .to(inventoryExchange)
                .with(INVENTORY_RESTOCK_ROUTING_KEY);
    }
    
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
    }
}