You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.0 KiB

  1. package com.example.demo.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.core.TopicExchange;
  6. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  7. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  8. import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
  9. import org.springframework.amqp.support.converter.MessageConverter;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. /**
  13. * RabbitMQ配置类
  14. * 配置消息队列交换机以及它们之间的绑定关系
  15. */
  16. @Configuration
  17. public class RabbitMQConfig {
  18. public static final String LOG_QUEUE = "operation_log_queue";
  19. public static final String LOG_EXCHANGE = "operation_log_exchange";
  20. // 收款流程相关队列和交换机常量定义
  21. public static final String CASH_COLLECTION_EXCHANGE = "cash_collection_exchange";
  22. public static final String COLLECTION_CREATED_QUEUE = "collection_created_queue";
  23. public static final String COLLECTION_AUDITED_QUEUE = "collection_audited_queue";
  24. public static final String COLLECTION_COMPLETED_QUEUE = "collection_completed_queue";
  25. public static final String COLLECTION_CANCELLED_QUEUE = "collection_cancelled_queue";
  26. public static final String COLLECTION_REJECTED_QUEUE = "collection_rejected_queue";
  27. // 退款流程相关队列和交换机常量定义
  28. public static final String CASH_REFUND_EXCHANGE = "cash_refund_exchange";
  29. public static final String REFUND_CREATED_QUEUE = "refund_created_queue";
  30. public static final String REFUND_REVIEWED_QUEUE = "refund_reviewed_queue";
  31. public static final String REFUND_EXECUTED_QUEUE = "refund_executed_queue";
  32. public static final String REFUND_COMPLETED_QUEUE = "refund_completed_queue";
  33. public static final String REFUND_REJECTED_QUEUE = "refund_rejected_queue";
  34. @Bean
  35. public Queue logQueue() {
  36. return new Queue(LOG_QUEUE, true);
  37. }
  38. @Bean
  39. public TopicExchange logExchange() {
  40. return new TopicExchange(LOG_EXCHANGE);
  41. }
  42. @Bean
  43. public Binding binding() {
  44. return BindingBuilder.bind(logQueue())
  45. .to(logExchange())
  46. .with("log.*");
  47. }
  48. /**
  49. * 创建收款流程交换机
  50. * @return TopicExchange对象
  51. */
  52. @Bean
  53. public TopicExchange cashCollectionExchange() {
  54. return new TopicExchange(CASH_COLLECTION_EXCHANGE);
  55. }
  56. /**
  57. * 创建收款创建队列
  58. * @return Queue对象
  59. */
  60. @Bean
  61. public Queue collectionCreatedQueue() {
  62. return new Queue(COLLECTION_CREATED_QUEUE, true);
  63. }
  64. /**
  65. * 创建收款审核队列
  66. * @return Queue对象
  67. */
  68. @Bean
  69. public Queue collectionAuditedQueue() {
  70. return new Queue(COLLECTION_AUDITED_QUEUE, true);
  71. }
  72. /**
  73. * 创建收款完成队列
  74. * @return Queue对象
  75. */
  76. @Bean
  77. public Queue collectionCompletedQueue() {
  78. return new Queue(COLLECTION_COMPLETED_QUEUE, true);
  79. }
  80. /**
  81. * 创建收款取消队列
  82. * @return Queue对象
  83. */
  84. @Bean
  85. public Queue collectionCancelledQueue() {
  86. return new Queue(COLLECTION_CANCELLED_QUEUE, true);
  87. }
  88. /**
  89. * 创建收款拒绝队列
  90. * @return Queue对象
  91. */
  92. @Bean
  93. public Queue collectionRejectedQueue() {
  94. return new Queue(COLLECTION_REJECTED_QUEUE, true);
  95. }
  96. /**
  97. * 创建退款流程交换机
  98. * @return TopicExchange对象
  99. */
  100. @Bean
  101. public TopicExchange cashRefundExchange() {
  102. return new TopicExchange(CASH_REFUND_EXCHANGE);
  103. }
  104. /**
  105. * 创建退款创建队列
  106. * @return Queue对象
  107. */
  108. @Bean
  109. public Queue refundCreatedQueue() {
  110. return new Queue(REFUND_CREATED_QUEUE, true);
  111. }
  112. /**
  113. * 创建退款审核队列
  114. * @return Queue对象
  115. */
  116. @Bean
  117. public Queue refundReviewedQueue() {
  118. return new Queue(REFUND_REVIEWED_QUEUE, true);
  119. }
  120. /**
  121. * 创建退款执行队列
  122. * @return Queue对象
  123. */
  124. @Bean
  125. public Queue refundExecutedQueue() {
  126. return new Queue(REFUND_EXECUTED_QUEUE, true);
  127. }
  128. /**
  129. * 创建退款完成队列
  130. * @return Queue对象
  131. */
  132. @Bean
  133. public Queue refundCompletedQueue() {
  134. return new Queue(REFUND_COMPLETED_QUEUE, true);
  135. }
  136. /**
  137. * 创建退款拒绝队列
  138. * @return Queue对象
  139. */
  140. @Bean
  141. public Queue refundRejectedQueue() {
  142. return new Queue(REFUND_REJECTED_QUEUE, true);
  143. }
  144. /**
  145. * 绑定收款创建队列到收款交换机
  146. * @return Binding对象
  147. */
  148. @Bean
  149. public Binding collectionCreatedBinding() {
  150. return BindingBuilder.bind(collectionCreatedQueue())
  151. .to(cashCollectionExchange())
  152. .with("collection.created");
  153. }
  154. /**
  155. * 绑定收款审核队列到收款交换机
  156. * @return Binding对象
  157. */
  158. @Bean
  159. public Binding collectionAuditedBinding() {
  160. return BindingBuilder.bind(collectionAuditedQueue())
  161. .to(cashCollectionExchange())
  162. .with("collection.audited");
  163. }
  164. /**
  165. * 绑定收款完成队列到收款交换机
  166. * @return Binding对象
  167. */
  168. @Bean
  169. public Binding collectionCompletedBinding() {
  170. return BindingBuilder.bind(collectionCompletedQueue())
  171. .to(cashCollectionExchange())
  172. .with("collection.completed");
  173. }
  174. /**
  175. * 绑定收款取消队列到收款交换机
  176. * @return Binding对象
  177. */
  178. @Bean
  179. public Binding collectionCancelledBinding() {
  180. return BindingBuilder.bind(collectionCancelledQueue())
  181. .to(cashCollectionExchange())
  182. .with("collection.cancelled");
  183. }
  184. /**
  185. * 绑定收款拒绝队列到收款交换机
  186. * @return Binding对象
  187. */
  188. @Bean
  189. public Binding collectionRejectedBinding() {
  190. return BindingBuilder.bind(collectionRejectedQueue())
  191. .to(cashCollectionExchange())
  192. .with("collection.rejected");
  193. }
  194. /**
  195. * 绑定退款创建队列到退款交换机
  196. * @return Binding对象
  197. */
  198. @Bean
  199. public Binding refundCreatedBinding() {
  200. return BindingBuilder.bind(refundCreatedQueue())
  201. .to(cashRefundExchange())
  202. .with("refund.created");
  203. }
  204. /**
  205. * 绑定退款审核队列到退款交换机
  206. * @return Binding对象
  207. */
  208. @Bean
  209. public Binding refundReviewedBinding() {
  210. return BindingBuilder.bind(refundReviewedQueue())
  211. .to(cashRefundExchange())
  212. .with("refund.reviewed");
  213. }
  214. /**
  215. * 绑定退款执行队列到退款交换机
  216. * @return Binding对象
  217. */
  218. @Bean
  219. public Binding refundExecutedBinding() {
  220. return BindingBuilder.bind(refundExecutedQueue())
  221. .to(cashRefundExchange())
  222. .with("refund.executed");
  223. }
  224. /**
  225. * 绑定退款完成队列到退款交换机
  226. * @return Binding对象
  227. */
  228. @Bean
  229. public Binding refundCompletedBinding() {
  230. return BindingBuilder.bind(refundCompletedQueue())
  231. .to(cashRefundExchange())
  232. .with("refund.completed");
  233. }
  234. /**
  235. * 绑定退款拒绝队列到退款交换机
  236. * @return Binding对象
  237. */
  238. @Bean
  239. public Binding refundRejectedBinding() {
  240. return BindingBuilder.bind(refundRejectedQueue())
  241. .to(cashRefundExchange())
  242. .with("refund.rejected");
  243. }
  244. @Bean
  245. public MessageConverter messageConverter() {
  246. return new Jackson2JsonMessageConverter();
  247. }
  248. @Bean
  249. public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
  250. RabbitTemplate template = new RabbitTemplate(connectionFactory);
  251. template.setMessageConverter(messageConverter());
  252. return template;
  253. }
  254. }