|
| 1 | +package com.mayank.order_service.config; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.mayank.order_service.ApplicationProperties; |
| 5 | +import org.springframework.amqp.core.Binding; |
| 6 | +import org.springframework.amqp.core.BindingBuilder; |
| 7 | +import org.springframework.amqp.core.DirectExchange; |
| 8 | +import org.springframework.amqp.core.Queue; |
| 9 | +import org.springframework.amqp.core.QueueBuilder; |
| 10 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 11 | +import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 12 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 13 | +import org.springframework.amqp.support.converter.JacksonJsonMessageConverter; |
| 14 | +import org.springframework.boot.ApplicationRunner; |
| 15 | +import org.springframework.context.annotation.Bean; |
| 16 | +import org.springframework.context.annotation.Configuration; |
| 17 | + |
| 18 | +@Configuration |
| 19 | +class RabbitMQConfig { |
| 20 | + private final ApplicationProperties properties; |
| 21 | + |
| 22 | + RabbitMQConfig(ApplicationProperties properties) { |
| 23 | + this.properties = properties; |
| 24 | + } |
| 25 | + |
| 26 | + @Bean |
| 27 | + DirectExchange exchange() { |
| 28 | + return new DirectExchange(properties.orderEventsExchange()); |
| 29 | + } |
| 30 | + |
| 31 | + @Bean |
| 32 | + Queue newOrdersQueue() { |
| 33 | + return QueueBuilder.durable(properties.newOrdersQueue()).build(); |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + Binding newOrdersQueueBinding() { |
| 38 | + return BindingBuilder.bind(newOrdersQueue()).to(exchange()).with(properties.newOrdersQueue()); |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + Queue deliveredOrdersQueue() { |
| 43 | + return QueueBuilder.durable(properties.deliveredOrdersQueue()).build(); |
| 44 | + } |
| 45 | + |
| 46 | + @Bean |
| 47 | + Binding deliveredOrdersQueueBinding() { |
| 48 | + return BindingBuilder.bind(deliveredOrdersQueue()).to(exchange()).with(properties.deliveredOrdersQueue()); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean |
| 52 | + Queue cancelledOrdersQueue() { |
| 53 | + return QueueBuilder.durable(properties.cancelledOrdersQueue()).build(); |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + Binding cancelledOrdersQueueBinding() { |
| 58 | + return BindingBuilder.bind(cancelledOrdersQueue()).to(exchange()).with(properties.cancelledOrdersQueue()); |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + Queue errorOrdersQueue() { |
| 63 | + return QueueBuilder.durable(properties.errorOrdersQueue()).build(); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + Binding errorOrdersQueueBinding() { |
| 68 | + return BindingBuilder.bind(errorOrdersQueue()).to(exchange()).with(properties.errorOrdersQueue()); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, JacksonJsonMessageConverter converter) { |
| 73 | + final var rabbitTemplate = new RabbitTemplate(connectionFactory); |
| 74 | + rabbitTemplate.setMessageConverter(converter); |
| 75 | + return rabbitTemplate; |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + public JacksonJsonMessageConverter jacksonConverter() { |
| 80 | + return new JacksonJsonMessageConverter(); |
| 81 | + } |
| 82 | + @Bean |
| 83 | + public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) { |
| 84 | + RabbitAdmin admin = new RabbitAdmin(connectionFactory); |
| 85 | + admin.setAutoStartup(true); |
| 86 | + return admin; |
| 87 | + } |
| 88 | + @Bean |
| 89 | + ApplicationRunner rabbitInitializer(RabbitAdmin rabbitAdmin) { |
| 90 | + return args -> rabbitAdmin.initialize(); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments