From b7fdd6af1655af3066afb68638450bcedf73d6a7 Mon Sep 17 00:00:00 2001 From: dhslrl321 Date: Sun, 2 Oct 2022 23:25:59 +0900 Subject: [PATCH] add registrable --- wiki/Getting Started-eng.md | 247 ++++++++++++++++++ ...ting Started.md => Getting Started-kor.md} | 0 .../converter/CompositeMessageConverter.java | 2 +- .../converter/MessageConverterRegistrar.java | 5 - .../dhslrl321/zsmq/converter/Registrable.java | 5 + ...SpringBeanMessageListenerDetectorTest.java | 6 - 6 files changed, 253 insertions(+), 12 deletions(-) create mode 100644 wiki/Getting Started-eng.md rename wiki/{Getting Started.md => Getting Started-kor.md} (100%) delete mode 100644 zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/MessageConverterRegistrar.java create mode 100644 zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/Registrable.java diff --git a/wiki/Getting Started-eng.md b/wiki/Getting Started-eng.md new file mode 100644 index 0000000..872e67e --- /dev/null +++ b/wiki/Getting Started-eng.md @@ -0,0 +1,247 @@ +#### You can see the full source code at [zsmq-example.git](https://github.com/dhslrl321/zsmq-example). + +# Implements a Application that use Messaging Infrastructure in 5 Minutes + +Let's make a application that use messaging platform in five minutes with ZSMQ. + +The example implements the following situations: + +image + +- **order-service** publish two events. + 1. `ORDER-CONFIRMED` + 2. `ORDER-CANCELED` +- **delivery-service** consume two events. + - `ORDER-CONFIRMED` 's payload is java object + - `ORDER-CANCELED` 's payload is simple text (string) + +# Step + +The configuration order follows the quickstart's step in [README](https://github.com/dhslrl321/zsmq) + +1. run messaging server and dashboard +2. gradle dependency +3. configure property +4. just U.S.E it + +# 1. run messaging server and dashboard + +First, run the messaging server and the dashboard using the docker. + +image + +Make sure it's done well. + +# 2. gradle dependency + +gradle 의존성을 추가하기 위해서 두개의 spring application 을 생성해줍니다. + +Create two spring applications. + +image + +and add gradle dependencies to `build.gradle` + +image + +check the [release note](https://github.com/dhslrl321/zsmq/releases) for version + +# 3. configure property + +The two applications have different uses. + +1. order-service (produce) +2. delivery-service (consume) + +## order-service, produce + +order-service is an application that publish a message. + +Set 'application.yml' as follows + +image + +Writing 'zsmq.listening' as false means that you will not run the message listener thread. + +#### Now let's implemnts an application! + +### Order.java + +Implement simple Order objects. We will forward the Order object to 'delivery-service'. + +```java +@Value(staticConstructor = "of") +public class Order { + String orderId; + String address; + int price; +} +``` + +### MessagePublisher.java + +And implements a simple message publisher. + +```java +@Component +@RequiredArgsConstructor +public class MessagePublisher { + private final ZolaQueueMessageTemplate template; + + public void sendConfirmedMessage(Order order) { + template.convertAndSend("ORDER-CONFIRMED", order); + } + + public void sendCanceledMessage(String orderId) { + template.convertAndSend("ORDER-CANCELED", orderId); + } +} +``` + +You can easily convert and send using `ZolaQueueMessageTemplate` + +You can forward it in object format or in simple string format. + +### OrderController.java + +```java +@RestController +@RequiredArgsConstructor +public class OrderController { + + private static final Order[] SAMPLE_ORDERS = { + Order.of(UUID.randomUUID().toString(), "Seoul", 125_000_000), + Order.of(UUID.randomUUID().toString(), "New York", 25_602_900), + Order.of(UUID.randomUUID().toString(), "singapore", 5_120_000), + Order.of(UUID.randomUUID().toString(), "tokyo", 9_000_000), + }; + + private final MessagePublisher publisher; + + @GetMapping("/orders/{index}/confirm") + public boolean confirm(@PathVariable int index) { + try { + Order order = SAMPLE_ORDERS[index]; + publisher.sendConfirmedMessage(order); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + @GetMapping("/orders/{index}/cancel") + public boolean cancel(@PathVariable int index) { + try { + String orderId = SAMPLE_ORDERS[index].getOrderId(); + publisher.sendCanceledMessage(orderId); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} +``` + +Let's implement a simple controller. + +There are two apis here. + +1. `orders/{index}/confirm` : publish objects +2. `orders/{index}/cancel` : publish text, string + +We are now ready to publish the message. + +## delivery-service, consume + +Because `delivery-service` will consume the message, the yml listening thread must be activated as follows + +image + +set `zsmq.listening` true + +Create the next two processors to consuming + +### OrderCanceledProcessor.java + +when `order-service` s `orders/{index}/cancel` are called, this will works + +```java +@Component +@ZolaConsumer +@Slf4j +public class OrderCanceledProcessor { + @ZolaMessageListener(queueName="ORDER-CANCELED", deletionPolicy = DeletionPolicy.ALWAYS) + public void listen(String message) { + log.info("order was canceled id: [{}]", message); + } +} +``` + +When using '@ZolaMessageListener', the parameters of the method must be String type and single argument. + +Pass consumed data with the corresponding parameters. + +### OrderConfirmedProcessor.java + +when `order-service` s `orders/{index}/confirm` are called, this will works + +```java +@Component +@ZolaConsumer +@Slf4j +public class OrderConfirmedProcessor { + + @ZolaMessageListener(queueName="ORDER-CONFIRMED", deletionPolicy = DeletionPolicy.ALWAYS) + public void listen(String message) { + log.info("json : {}", message); + Order order = ZolaJsonSerializer.getInstance().deserialize(message, Order.class); + log.info("order was confirmed : id : {}, address: {}, price: {}", order.getOrderId(), order.getAddress(), order.getPrice()); + } +} +``` + +If you publish the object, you will receive the data in the form of json. + +'ZolaJsonSerializer' provided by zsmq makes it easy to deserialize json. + +# Let's run! + +All preparations are now complete. + +Let's check the results + +First, you need to create a queue. + +Access the dashboard running `localhost:8290` + +image + +We need to create two queues. Click on the Create QUEUE menu on the tab on the right and add a queue + +image + +Add both queues. + +image + +Run `order-service` 와 `delivery-service` application + +and `order-service` request api! + +image + +Since the delivery-service was previously required to output the log, you can see that the log is taken as shown below. + +image + +One is printed as it is in json form, and the other is printed as it is taken out of the object. + +Now if you call the rest of the cancel, you will get the same result. + +image + +image + +#### You can see the full source code at [zsmq-example.git](https://github.com/dhslrl321/zsmq-example). diff --git a/wiki/Getting Started.md b/wiki/Getting Started-kor.md similarity index 100% rename from wiki/Getting Started.md rename to wiki/Getting Started-kor.md diff --git a/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/CompositeMessageConverter.java b/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/CompositeMessageConverter.java index d12f581..c94fa76 100644 --- a/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/CompositeMessageConverter.java +++ b/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/CompositeMessageConverter.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; -class CompositeMessageConverter implements MessageConverter, MessageConverterRegistrar { +class CompositeMessageConverter implements MessageConverter, Registrable { private final List registeredConverters = new ArrayList<>(); @Override diff --git a/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/MessageConverterRegistrar.java b/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/MessageConverterRegistrar.java deleted file mode 100644 index 952d0a4..0000000 --- a/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/MessageConverterRegistrar.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.dhslrl321.zsmq.converter; - -public interface MessageConverterRegistrar { - void register(MessageConverter converter); -} diff --git a/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/Registrable.java b/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/Registrable.java new file mode 100644 index 0000000..f2cdde9 --- /dev/null +++ b/zola-messaging-core/src/main/java/com/github/dhslrl321/zsmq/converter/Registrable.java @@ -0,0 +1,5 @@ +package com.github.dhslrl321.zsmq.converter; + +public interface Registrable { + void register(T t); +} diff --git a/zola-messaging-spring-sdk/src/test/java/com/github/dhslrl321/zsmq/detector/SpringBeanMessageListenerDetectorTest.java b/zola-messaging-spring-sdk/src/test/java/com/github/dhslrl321/zsmq/detector/SpringBeanMessageListenerDetectorTest.java index c23627f..30f46cd 100644 --- a/zola-messaging-spring-sdk/src/test/java/com/github/dhslrl321/zsmq/detector/SpringBeanMessageListenerDetectorTest.java +++ b/zola-messaging-spring-sdk/src/test/java/com/github/dhslrl321/zsmq/detector/SpringBeanMessageListenerDetectorTest.java @@ -27,14 +27,8 @@ class SpringBeanMessageListenerDetectorTest { SpringBeanMessageListenerDetector sut; - ListenerBeanFinder finder = mock(ListenerBeanFinder.class); ZolaClientConfig config = mock(ZolaClientConfig.class); - /*@BeforeEach - void setUp() { - sut = new SpringBeanMessageListenerDetector(finder, config); - }*/ - @EqualsAndHashCode @ZolaConsumer static class Foo {