RabbitMQConfig.java
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
}
}