From 3f14e92fe45e01571082bf4f3d67077895f7b145 Mon Sep 17 00:00:00 2001 From: Guilherme Biff Zarelli Date: Fri, 24 May 2019 00:04:23 -0300 Subject: [PATCH 1/3] feat(rabbitmq): Init - Init with RabbitMQ - docker-compose configured - Init in spring POM and yml Took 53 minutes --- docker-compose.yml | 13 +++++++++++++ pom.xml | 4 ++++ .../musicstore/MusicStoreApplication.java | 14 ++++++++++++++ .../beblue/musicstore/amqp/SaleQueueSender.java | 14 ++++++++++++++ .../beblue/musicstore/service/SaleService.java | 17 ++++++++++++++--- .../beblue/musicstore/spotify/SpotifyAPI.java | 7 +++++-- .../com/beblue/musicstore/util/BeanConst.java | 5 +++++ .../beblue/musicstore/util/ResourceConst.java | 2 ++ src/main/resources/application-prod.yml | 5 ++++- src/main/resources/application.yml | 12 +++++++++++- 10 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java create mode 100644 src/main/java/br/com/beblue/musicstore/util/BeanConst.java diff --git a/docker-compose.yml b/docker-compose.yml index ea6fbb8..ed14cf2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,19 @@ version: '3' services: + rabbitmq: + container_name: rabbit-mq + image: rabbitmq:3-management-alpine + ports: + - "15672:15672" + - "5672:5672" + networks: + - musicstore-network + environment: + RABBITMQ_DEFAULT_USER: rabbitmq + RABBITMQ_DEFAULT_PASS: rabbitmq + RABBITMQ_DEFAULT_VHOST: rabbitmq + mysql: image: mysql:5.7 container_name: music-store-mysql diff --git a/pom.xml b/pom.xml index a425de4..b9ea2d9 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,10 @@ ${spotify.web-api.version} + + org.springframework.boot + spring-boot-starter-amqp + org.springframework.boot diff --git a/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java b/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java index b66844f..8c38f5b 100644 --- a/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java +++ b/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java @@ -1,7 +1,13 @@ package br.com.beblue.musicstore; +import org.springframework.amqp.core.Queue; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_QUEUE; +import static br.com.beblue.musicstore.util.ResourceConst.KEY_APPLICATION_MQ_QUEUE_SALE_NAME; @SpringBootApplication public class MusicStoreApplication { @@ -10,4 +16,12 @@ public static void main(String[] args) { SpringApplication.run(MusicStoreApplication.class, args); } + @Value(KEY_APPLICATION_MQ_QUEUE_SALE_NAME) + private String queueSaleName; + + @Bean(BEAN_SALE_QUEUE) + Queue getAmqQueue() { + return new Queue(queueSaleName, true); + } + } diff --git a/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java b/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java new file mode 100644 index 0000000..1c2b2fb --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java @@ -0,0 +1,14 @@ +package br.com.beblue.musicstore.amqp; + +import org.springframework.stereotype.Component; + +@Component +public class SaleQueueSender { + + //TODO + + public void send(String order) { + //TODO + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/service/SaleService.java b/src/main/java/br/com/beblue/musicstore/service/SaleService.java index 0a05241..6c37ba0 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SaleService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SaleService.java @@ -1,5 +1,6 @@ package br.com.beblue.musicstore.service; +import br.com.beblue.musicstore.amqp.SaleQueueSender; import br.com.beblue.musicstore.dto.SaleRequestDTO; import br.com.beblue.musicstore.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; @@ -9,7 +10,6 @@ import br.com.beblue.musicstore.model.entity.SaleEntity; import br.com.beblue.musicstore.model.repository.DiscRepository; import br.com.beblue.musicstore.model.repository.SaleRepository; -import br.com.beblue.musicstore.util.converter.DiscSaleConverter; import br.com.beblue.musicstore.util.converter.SaleConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,12 +22,14 @@ public class SaleService { private final SaleRepository saleRepository; private final DiscRepository discRepository; private final CashbackService cashbackService; + private final SaleQueueSender saleQueueSender; @Autowired - public SaleService(SaleRepository saleRepository, DiscRepository discRepository, CashbackService cashbackService) { + public SaleService(SaleRepository saleRepository, DiscRepository discRepository, CashbackService cashbackService, SaleQueueSender saleQueueSender) { this.saleRepository = saleRepository; this.discRepository = discRepository; this.cashbackService = cashbackService; + this.saleQueueSender = saleQueueSender; } public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentException { @@ -52,11 +54,20 @@ public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentE saleEntity.addDiscSaleEntity(discSaleEntity); }); - saleRepository.save(saleEntity); + persist(saleEntity); + notifyMessageQueue(saleEntity); return SaleConverter.saleEntityToSaleResponseDTO(saleEntity); } + private void notifyMessageQueue(SaleEntity saleEntity) { + saleQueueSender.send(saleEntity.getUuid()); + } + + private void persist(SaleEntity saleEntity) { + saleRepository.save(saleEntity); + } + private double getCashbackPrice(double price, Integer cashback) { return (price * cashback) / 100; } diff --git a/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java b/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java index 6b1ba2b..a1f666e 100644 --- a/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java +++ b/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java @@ -10,6 +10,9 @@ import java.io.IOException; import java.util.Objects; +import static br.com.beblue.musicstore.util.ResourceConst.KEY_SPOTIFY_CLIENT_ID; +import static br.com.beblue.musicstore.util.ResourceConst.KEY_SPOTIFY_CLIENT_SECRET; + @Component public class SpotifyAPI { @@ -18,8 +21,8 @@ public class SpotifyAPI { private final String clientSecret; private Credentials credentials; - public SpotifyAPI(@Value("${spotify.client.id}") String clientId, - @Value("${spotify.client.secret}") String clientSecret + public SpotifyAPI(@Value(KEY_SPOTIFY_CLIENT_ID) String clientId, + @Value(KEY_SPOTIFY_CLIENT_SECRET) String clientSecret ) { this.clientId = clientId; this.clientSecret = clientSecret; diff --git a/src/main/java/br/com/beblue/musicstore/util/BeanConst.java b/src/main/java/br/com/beblue/musicstore/util/BeanConst.java new file mode 100644 index 0000000..1e39542 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/util/BeanConst.java @@ -0,0 +1,5 @@ +package br.com.beblue.musicstore.util; + +public class BeanConst { + public static final String BEAN_SALE_QUEUE="saleQueue"; +} diff --git a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java index 728c2a3..982d72f 100644 --- a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java +++ b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java @@ -3,5 +3,7 @@ public class ResourceConst { public static final String ACTIVE_PROFILES_TEST_VALUE = "test"; public static final String KEY_SPOTIFY_CLIENT_ID = "${spotify.client.id}"; + public static final String KEY_SPOTIFY_CLIENT_SECRET = "${spotify.client.secret}"; public static final String KEY_APPLICATION_ENV = "${application.environment}"; + public static final String KEY_APPLICATION_MQ_QUEUE_SALE_NAME = "${application.mq.queue.sale.name}"; } diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 514e793..07f1502 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -13,4 +13,7 @@ spring: jpa: properties: hibernate: - show_sql: false \ No newline at end of file + show_sql: false + + rabbitmq: + host: rabbitmq \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7fd0038..8d3a10e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,6 +8,11 @@ application: version: @project.version@ environment: default + mq: + queue: + sale: + name: music-store-sales + spotify: client: id: 1228515c3a654087b2a33907226e09e9 @@ -33,4 +38,9 @@ spring: user: ${spring.datasource.username} password: ${spring.datasource.password} baseline-on-migrate: true - validate-on-migrate: false \ No newline at end of file + validate-on-migrate: false + + rabbitmq: + host: localhost + username: rabbitmq + password: rabbitmq \ No newline at end of file From b950bac4707a1963c85c60d31f24ff28ad501e4a Mon Sep 17 00:00:00 2001 From: Guilherme Biff Zarelli Date: Sat, 25 May 2019 13:12:03 -0300 Subject: [PATCH 2/3] feat(rabbitmq): Send to AMQ - Configure AMQP (RabbitMQ) - Send message when sale - Readme Took 1 hour 49 minutes --- README.md | 8 +++- docker-compose.yml | 1 - pom.xml | 2 +- .../musicstore/MusicStoreApplication.java | 13 ------ .../beblue/musicstore/amq/SaleAMQSender.java | 32 +++++++++++++ .../musicstore/amqp/SaleQueueSender.java | 14 ------ .../musicstore/service/SaleService.java | 10 ++-- .../musicstore/settings/amq/SaleConfig.java | 46 +++++++++++++++++++ .../com/beblue/musicstore/util/BeanConst.java | 3 +- .../beblue/musicstore/util/ResourceConst.java | 4 +- src/main/resources/application.yml | 9 ++-- .../musicstore/service/SaleServiceTest.java | 4 ++ 12 files changed, 105 insertions(+), 41 deletions(-) create mode 100644 src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java delete mode 100644 src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java create mode 100644 src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java diff --git a/README.md b/README.md index f91b462..f8b8ed4 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,12 @@ - Consultar uma venda pelo seu identificador; - Registrar uma nova venda de discos calculando o valor total de cashback considerando a tabela. - O serviço será alimentado pela base de dados do `spotfy` [`web-api`](https://developer.spotify.com/documentation/web-api/quick-start/) + - O serviço será alimentado pela base de dados do `spotfy` [`web-api`](https://developer.spotify.com/documentation/web-api/quick-start/) + - Cada venda poderá ter 1 ou mais discos selecionados, o cashback deverá ser calculado e armazenado individualmente para cada disco bem como o cashback total da venda. - Cada venda poderá ter 1 ou mais discos selecionados, o cashback deverá ser calculado e armazenado individualmente para cada disco bem como o cashback total da venda. + Extra: + + - Ao registrar uma venda, a API deve enviar uma mensagem a um serviço de mensageria passando o número único da venda. ## Tecnologias @@ -32,6 +35,7 @@ - [`Swagger`](https://swagger.io) - Documentação de API de forma dinâmica - [`JPA/Hibernate`](https://hibernate.org/orm/) - Framework para persistencia de dados / ORM - [`Docker`](https://www.docker.com) - Executa e gerencia aplicações dentro de invólucros chamados containers + - [`RabbitMQ`](https://www.rabbitmq.com) - RabbitMQ é um servidor de mensageria - [`jUnit5 e Mockito`](https://junit.org/junit5/) - Execução de testes ## Executando com Docker em ambiente de desenvolvimento diff --git a/docker-compose.yml b/docker-compose.yml index ed14cf2..eccd630 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,6 @@ services: environment: RABBITMQ_DEFAULT_USER: rabbitmq RABBITMQ_DEFAULT_PASS: rabbitmq - RABBITMQ_DEFAULT_VHOST: rabbitmq mysql: image: mysql:5.7 diff --git a/pom.xml b/pom.xml index b9ea2d9..7b90ecc 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ br.com.beblue music-store-api - 0.0.1-SNAPSHOT + 1.1.0 Music Store API for querying and selling discs diff --git a/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java b/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java index 8c38f5b..bbb70bd 100644 --- a/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java +++ b/src/main/java/br/com/beblue/musicstore/MusicStoreApplication.java @@ -1,13 +1,8 @@ package br.com.beblue.musicstore; -import org.springframework.amqp.core.Queue; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; -import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_QUEUE; -import static br.com.beblue.musicstore.util.ResourceConst.KEY_APPLICATION_MQ_QUEUE_SALE_NAME; @SpringBootApplication public class MusicStoreApplication { @@ -16,12 +11,4 @@ public static void main(String[] args) { SpringApplication.run(MusicStoreApplication.class, args); } - @Value(KEY_APPLICATION_MQ_QUEUE_SALE_NAME) - private String queueSaleName; - - @Bean(BEAN_SALE_QUEUE) - Queue getAmqQueue() { - return new Queue(queueSaleName, true); - } - } diff --git a/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java b/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java new file mode 100644 index 0000000..463171d --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java @@ -0,0 +1,32 @@ +package br.com.beblue.musicstore.amq; + +import org.springframework.amqp.core.TopicExchange; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_EXCHANGE; +import static br.com.beblue.musicstore.util.ResourceConst.KEY_APPLICATION_MQ_SALE_ROUTING_KEY; + +@Component +public class SaleAMQSender { + + @Value(KEY_APPLICATION_MQ_SALE_ROUTING_KEY) + private String exchangeRoutingKey; + + private final RabbitTemplate rabbitTemplate; + private final TopicExchange topicExchange; + + @Autowired + public SaleAMQSender(RabbitTemplate rabbitTemplate, @Qualifier(BEAN_SALE_EXCHANGE) TopicExchange topicExchange) { + this.rabbitTemplate = rabbitTemplate; + this.topicExchange = topicExchange; + } + + public void send(String order) { + rabbitTemplate.convertAndSend(topicExchange.getName(), exchangeRoutingKey, order); + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java b/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java deleted file mode 100644 index 1c2b2fb..0000000 --- a/src/main/java/br/com/beblue/musicstore/amqp/SaleQueueSender.java +++ /dev/null @@ -1,14 +0,0 @@ -package br.com.beblue.musicstore.amqp; - -import org.springframework.stereotype.Component; - -@Component -public class SaleQueueSender { - - //TODO - - public void send(String order) { - //TODO - } - -} diff --git a/src/main/java/br/com/beblue/musicstore/service/SaleService.java b/src/main/java/br/com/beblue/musicstore/service/SaleService.java index 6c37ba0..1380d37 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SaleService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SaleService.java @@ -1,6 +1,6 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.amqp.SaleQueueSender; +import br.com.beblue.musicstore.amq.SaleAMQSender; import br.com.beblue.musicstore.dto.SaleRequestDTO; import br.com.beblue.musicstore.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; @@ -22,14 +22,14 @@ public class SaleService { private final SaleRepository saleRepository; private final DiscRepository discRepository; private final CashbackService cashbackService; - private final SaleQueueSender saleQueueSender; + private final SaleAMQSender saleAMQSender; @Autowired - public SaleService(SaleRepository saleRepository, DiscRepository discRepository, CashbackService cashbackService, SaleQueueSender saleQueueSender) { + public SaleService(SaleRepository saleRepository, DiscRepository discRepository, CashbackService cashbackService, SaleAMQSender saleAMQSender) { this.saleRepository = saleRepository; this.discRepository = discRepository; this.cashbackService = cashbackService; - this.saleQueueSender = saleQueueSender; + this.saleAMQSender = saleAMQSender; } public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentException { @@ -61,7 +61,7 @@ public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentE } private void notifyMessageQueue(SaleEntity saleEntity) { - saleQueueSender.send(saleEntity.getUuid()); + saleAMQSender.send(saleEntity.getUuid()); } private void persist(SaleEntity saleEntity) { diff --git a/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java b/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java new file mode 100644 index 0000000..360e97f --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java @@ -0,0 +1,46 @@ +package br.com.beblue.musicstore.settings.amq; + +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; + +import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_QUEUE; +import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_EXCHANGE; +import static br.com.beblue.musicstore.util.ResourceConst.*; + +@Configuration +public class SaleConfig { + + @Value(KEY_APPLICATION_MQ_SALE_QUEUE_NAME) + private String queueName; + + @Value(KEY_APPLICATION_MQ_SALE_EXCHANGE_NAME) + private String exchangeName; + + @Value(KEY_APPLICATION_MQ_SALE_ROUTING_KEY) + private String exchangeRoutingKey; + + @Bean(BEAN_SALE_QUEUE) + Queue queue() { + return new Queue(queueName, true); + } + + @Bean(BEAN_SALE_EXCHANGE) + TopicExchange exchange() { + return new TopicExchange(exchangeName); + } + + @Bean + @DependsOn({BEAN_SALE_QUEUE, BEAN_SALE_EXCHANGE}) + Binding binding(@Qualifier(BEAN_SALE_QUEUE) Queue queue, @Qualifier(BEAN_SALE_EXCHANGE) TopicExchange exchange) { + return BindingBuilder.bind(queue).to(exchange).with(exchangeRoutingKey); + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/util/BeanConst.java b/src/main/java/br/com/beblue/musicstore/util/BeanConst.java index 1e39542..5b2f326 100644 --- a/src/main/java/br/com/beblue/musicstore/util/BeanConst.java +++ b/src/main/java/br/com/beblue/musicstore/util/BeanConst.java @@ -1,5 +1,6 @@ package br.com.beblue.musicstore.util; public class BeanConst { - public static final String BEAN_SALE_QUEUE="saleQueue"; + public static final String BEAN_SALE_QUEUE = "saleQueue"; + public static final String BEAN_SALE_EXCHANGE = "saleExchange"; } diff --git a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java index 982d72f..4f26c81 100644 --- a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java +++ b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java @@ -5,5 +5,7 @@ public class ResourceConst { public static final String KEY_SPOTIFY_CLIENT_ID = "${spotify.client.id}"; public static final String KEY_SPOTIFY_CLIENT_SECRET = "${spotify.client.secret}"; public static final String KEY_APPLICATION_ENV = "${application.environment}"; - public static final String KEY_APPLICATION_MQ_QUEUE_SALE_NAME = "${application.mq.queue.sale.name}"; + public static final String KEY_APPLICATION_MQ_SALE_QUEUE_NAME = "${application.mq.sale.queue.name}"; + public static final String KEY_APPLICATION_MQ_SALE_EXCHANGE_NAME = "${application.mq.sale.exchange.name}"; + public static final String KEY_APPLICATION_MQ_SALE_ROUTING_KEY = "${application.mq.sale.exchange.routing-key}"; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8d3a10e..5e8dff1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,9 +9,12 @@ application: environment: default mq: - queue: - sale: - name: music-store-sales + sale: + queue: + name: sales + exchange: + name: sales-exchange + routing-key: musicstore.sales spotify: client: diff --git a/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java index 70b90f7..b5c8b40 100644 --- a/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java +++ b/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java @@ -1,5 +1,6 @@ package br.com.beblue.musicstore.service; +import br.com.beblue.musicstore.amq.SaleAMQSender; import br.com.beblue.musicstore.dto.SaleRequestDTO; import br.com.beblue.musicstore.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; @@ -41,6 +42,9 @@ class SaleServiceTest { @MockBean CashbackService cashbackService; + @MockBean + SaleAMQSender saleAMQSender; + @Test void test_no_value_present_exception_with_empty_list() { Assertions.assertThrows(NoValuePresentException.class, () -> { From 512453f19793f72ac07ed86ffd357f39bb0b60da Mon Sep 17 00:00:00 2001 From: Guilherme Biff Zarelli Date: Sat, 25 May 2019 13:18:37 -0300 Subject: [PATCH 3/3] feat(rabbitmq): Send to AMQ - 'try catch' in the amq message notification - Configure AMQP (RabbitMQ) - Send message when sale - Readme Took 4 minutes --- .../java/br/com/beblue/musicstore/amq/SaleAMQSender.java | 3 ++- .../br/com/beblue/musicstore/service/SaleService.java | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java b/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java index 463171d..3983360 100644 --- a/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java +++ b/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java @@ -1,5 +1,6 @@ package br.com.beblue.musicstore.amq; +import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +26,7 @@ public SaleAMQSender(RabbitTemplate rabbitTemplate, @Qualifier(BEAN_SALE_EXCHANG this.topicExchange = topicExchange; } - public void send(String order) { + public void send(String order) throws AmqpException { rabbitTemplate.convertAndSend(topicExchange.getName(), exchangeRoutingKey, order); } diff --git a/src/main/java/br/com/beblue/musicstore/service/SaleService.java b/src/main/java/br/com/beblue/musicstore/service/SaleService.java index 1380d37..00cd4ee 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SaleService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SaleService.java @@ -61,7 +61,13 @@ public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentE } private void notifyMessageQueue(SaleEntity saleEntity) { - saleAMQSender.send(saleEntity.getUuid()); + // Caso falte comunicação ou de erro de persistencia, o sistema não + //deve parar. Posteriormente tratar o Log de erro + try { + saleAMQSender.send(saleEntity.getUuid()); + } catch (Throwable t) { + t.printStackTrace(); + } } private void persist(SaleEntity saleEntity) {