diff --git a/spring-boot/request-response/client/build.gradle b/spring-boot/request-response/client/build.gradle index ef4a79c..b257ce3 100644 --- a/spring-boot/request-response/client/build.gradle +++ b/spring-boot/request-response/client/build.gradle @@ -30,6 +30,8 @@ dependencies { implementation 'com.fasterxml.jackson.core:jackson-core:2.9.6' implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.6' implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6' + + testCompile "org.testcontainers:rabbitmq:1.14.3" } test { diff --git a/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/EventPublisher.java b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/EventPublisher.java index ba1526e..d91a064 100644 --- a/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/EventPublisher.java +++ b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/EventPublisher.java @@ -25,7 +25,8 @@ public class EventPublisher { private final DirectExchange directExchange; - public EventPublisher(DirectExchange directExchange, RabbitTemplate template, AsyncRabbitTemplate asyncRabbitTemplate) { + public EventPublisher(DirectExchange directExchange, RabbitTemplate template, + AsyncRabbitTemplate asyncRabbitTemplate) { this.directExchange = directExchange; this.template = template; this.asyncRabbitTemplate = asyncRabbitTemplate; @@ -36,38 +37,41 @@ public class EventPublisher { String key = "vw"; Car car = Car.builder() .id(UUID.randomUUID()) - // TODO get random color and name. .color("white") .name("vw") .build(); LOGGER.info("Sending message with routing key {} and id {}", key, car.getId()); - ParameterizedTypeReference responseType = new ParameterizedTypeReference<>() { + ParameterizedTypeReference responseType + = new ParameterizedTypeReference<>() { }; - Registration registration = template.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType); + Registration registration = template.convertSendAndReceiveAsType( + directExchange.getName(), key, car, responseType); LOGGER.info("Message received: {}", registration); } + @Scheduled(fixedDelay = 3000, initialDelay = 1500) public void sendAsynchronously() { String key = "vw"; Car car = Car.builder() .id(UUID.randomUUID()) - // TODO get random color and name. - .color("white") - .name("vw") + .color("black") + .name("bmw") .build(); LOGGER.info("Sending message with routing key {} and id {}", key, car.getId()); - ParameterizedTypeReference responseType = new ParameterizedTypeReference<>() { + ParameterizedTypeReference responseType + = new ParameterizedTypeReference<>() { }; AsyncRabbitTemplate.RabbitConverterFuture future = - asyncRabbitTemplate.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType); + asyncRabbitTemplate.convertSendAndReceiveAsType( + directExchange.getName(), key, car, responseType); try { Registration registration = future.get(); LOGGER.info("Asynchronous message received: {}", registration); } catch (InterruptedException | ExecutionException e) { - LOGGER.error("Cannot get response."); + LOGGER.error("Cannot get response.", e); } } } diff --git a/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/PublisherConfiguration.java b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/PublisherConfiguration.java index c0b73ae..0857895 100644 --- a/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/PublisherConfiguration.java +++ b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/PublisherConfiguration.java @@ -7,10 +7,8 @@ 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; -import org.springframework.scheduling.annotation.EnableScheduling; @Configuration -@EnableScheduling public class PublisherConfiguration { diff --git a/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/SchedulingConfiguration.java b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/SchedulingConfiguration.java new file mode 100644 index 0000000..16c27cc --- /dev/null +++ b/spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/SchedulingConfiguration.java @@ -0,0 +1,15 @@ +package io.reflectoring.client.rpc; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@ConditionalOnProperty( + value = "scheduling.enable", + havingValue = "true", + matchIfMissing = true +) +@Configuration +@EnableScheduling +public class SchedulingConfiguration { +} diff --git a/spring-boot/request-response/client/src/test/java/io/reflectoring/client/rpc/EventPublisherTest.java b/spring-boot/request-response/client/src/test/java/io/reflectoring/client/rpc/EventPublisherTest.java new file mode 100644 index 0000000..c4c42cb --- /dev/null +++ b/spring-boot/request-response/client/src/test/java/io/reflectoring/client/rpc/EventPublisherTest.java @@ -0,0 +1,48 @@ +package io.reflectoring.client.rpc; + +import org.assertj.core.api.ThrowableAssert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.testcontainers.containers.RabbitMQContainer; + +import static org.assertj.core.api.Assertions.assertThatCode; + +@SpringBootTest +@TestPropertySource(properties = "scheduling.enable=false") +class EventPublisherTest { + + private final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer(); + + @Autowired + private EventPublisher eventPublisher; + + @BeforeEach + void setUp() { + rabbitMQContainer.start(); + } + + @Test + void sendMessageSynchronously() { + // given + + // when + ThrowableAssert.ThrowingCallable send = () -> eventPublisher.send(); + + // then + assertThatCode(send).doesNotThrowAnyException(); + } + + @Test + void sendMessageAsynchronously() { + // given + + // when + ThrowableAssert.ThrowingCallable send = () -> eventPublisher.sendAsynchronously(); + + // then + assertThatCode(send).doesNotThrowAnyException(); + } +} \ No newline at end of file