RabbitMQConfig.java
6.93 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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;
}
}