RabbitMQConfig.java 4.53 KB
package com.ecommerce.payment.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 PAYMENT_EXCHANGE = "payment.exchange";
    public static final String ORDER_EXCHANGE = "order.exchange";
    
    // Queue names
    public static final String PAYMENT_SUCCESS_QUEUE = "payment.success.queue";
    public static final String PAYMENT_FAILED_QUEUE = "payment.failed.queue";
    public static final String PAYMENT_CANCELLED_QUEUE = "payment.cancelled.queue";
    public static final String REFUND_SUCCESS_QUEUE = "refund.success.queue";
    public static final String REFUND_FAILED_QUEUE = "refund.failed.queue";
    public static final String ORDER_CREATED_QUEUE = "payment.order.created.queue";
    
    // Routing keys
    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_CANCELLED_ROUTING_KEY = "payment.cancelled";
    public static final String REFUND_SUCCESS_ROUTING_KEY = "refund.success";
    public static final String REFUND_FAILED_ROUTING_KEY = "refund.failed";
    public static final String ORDER_CREATED_ROUTING_KEY = "order.created";
    
    @Bean
    public TopicExchange paymentExchange() {
        return new TopicExchange(PAYMENT_EXCHANGE);
    }
    
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(ORDER_EXCHANGE);
    }
    
    @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 paymentCancelledQueue() {
        return new Queue(PAYMENT_CANCELLED_QUEUE, true);
    }
    
    @Bean
    public Queue refundSuccessQueue() {
        return new Queue(REFUND_SUCCESS_QUEUE, true);
    }
    
    @Bean
    public Queue refundFailedQueue() {
        return new Queue(REFUND_FAILED_QUEUE, true);
    }
    
    @Bean
    public Queue orderCreatedQueue() {
        return new Queue(ORDER_CREATED_QUEUE, true);
    }
    
    @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 paymentCancelledBinding(Queue paymentCancelledQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(paymentCancelledQueue)
                .to(paymentExchange)
                .with(PAYMENT_CANCELLED_ROUTING_KEY);
    }
    
    @Bean
    public Binding refundSuccessBinding(Queue refundSuccessQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(refundSuccessQueue)
                .to(paymentExchange)
                .with(REFUND_SUCCESS_ROUTING_KEY);
    }
    
    @Bean
    public Binding refundFailedBinding(Queue refundFailedQueue, TopicExchange paymentExchange) {
        return BindingBuilder.bind(refundFailedQueue)
                .to(paymentExchange)
                .with(REFUND_FAILED_ROUTING_KEY);
    }
    
    @Bean
    public Binding orderCreatedBinding(Queue orderCreatedQueue, TopicExchange orderExchange) {
        return BindingBuilder.bind(orderCreatedQueue)
                .to(orderExchange)
                .with(ORDER_CREATED_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;
    }
}