Changeset 4:2da9d14cc378

Updated handling of filters
author unexist
date Sun, 14 Feb 2021 20:53:27 +0100
parents 69623799748c
children 3fa820d4f91a
files src/main/java/dev/unexist/showcase/todo/adapters/TodoConsumer.java src/main/java/dev/unexist/showcase/todo/adapters/TodoProducer.java src/main/java/dev/unexist/showcase/todo/adapters/TodoResource.java src/main/java/dev/unexist/showcase/todo/domain/todo/Todo.java src/main/java/dev/unexist/showcase/todo/domain/todo/TodoDtoAssembler.java src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoConverted.java src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoCreated.java src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoSaved.java src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoConversionFilter.java src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoPersistenceFilter.java src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseEvent.java src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseFilter.java src/main/resources/application.properties
diffstat 13 files changed, 117 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/dev/unexist/showcase/todo/adapters/TodoConsumer.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/adapters/TodoConsumer.java	Sun Feb 14 20:53:27 2021 +0100
@@ -11,20 +11,31 @@
 
 package dev.unexist.showcase.todo.adapters;
 
+import dev.unexist.showcase.todo.domain.todo.TodoDto;
+import dev.unexist.showcase.todo.domain.todo.events.TodoCreated;
 import org.eclipse.microprofile.reactive.messaging.Acknowledgment;
 import org.eclipse.microprofile.reactive.messaging.Incoming;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Event;
+import javax.inject.Inject;
 
 @ApplicationScoped
 public class TodoConsumer {
     private static final Logger LOGGER = LoggerFactory.getLogger(TodoConsumer.class);
 
+    @Inject
+    Event<TodoCreated> event;
+
     @Incoming("todo-in")
     @Acknowledgment(Acknowledgment.Strategy.PRE_PROCESSING)
-    public void receive(String value) {
-        LOGGER.info("Value: {}", value);
+    public void receive(TodoDto dto) {
+        if (null != dto) {
+            this.event.fire(new TodoCreated(dto));
+
+            LOGGER.info("Sent event={}", TodoCreated.class.getSimpleName());
+        }
     }
 }
--- a/src/main/java/dev/unexist/showcase/todo/adapters/TodoProducer.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/adapters/TodoProducer.java	Sun Feb 14 20:53:27 2021 +0100
@@ -11,21 +11,44 @@
 
 package dev.unexist.showcase.todo.adapters;
 
-import io.smallrye.mutiny.Multi;
+import dev.unexist.showcase.todo.domain.todo.Todo;
+import dev.unexist.showcase.todo.domain.todo.TodoDto;
+import dev.unexist.showcase.todo.domain.todo.TodoDtoAssembler;
+import dev.unexist.showcase.todo.domain.todo.events.TodoSaved;
+import io.smallrye.reactive.messaging.annotations.Broadcast;
 import org.eclipse.microprofile.reactive.messaging.Outgoing;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.enterprise.context.ApplicationScoped;
-
-import static io.smallrye.mutiny.Multi.createFrom;
+import javax.enterprise.event.Observes;
+import java.util.Optional;
 
 @ApplicationScoped
 public class TodoProducer {
     private static final Logger LOGGER = LoggerFactory.getLogger(TodoConsumer.class);
 
+    private TodoDto dto;
+
+    public void process(@Observes TodoSaved event) {
+        LOGGER.info("Received event={}", event.getClass().getSimpleName());
+
+        Optional<Todo> payload = event.getPayload();
+
+        if (payload.isPresent()) {
+            Todo todo = payload.get();
+
+            LOGGER.info("Received event payload={}", todo);
+
+            this.dto = TodoDtoAssembler.fromTodoToDto(todo);
+
+            this.send();
+        }
+    }
+
     @Outgoing("todo-out")
-    public Multi<String> send() {
-        return createFrom().item("Todo");
+    @Broadcast
+    public TodoDto send() {
+        return this.dto;
     }
-}
+}
\ No newline at end of file
--- a/src/main/java/dev/unexist/showcase/todo/adapters/TodoResource.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/adapters/TodoResource.java	Sun Feb 14 20:53:27 2021 +0100
@@ -13,10 +13,13 @@
 
 import dev.unexist.showcase.todo.domain.todo.TodoDto;
 import dev.unexist.showcase.todo.domain.todo.events.TodoCreated;
+import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseFilter;
 import org.eclipse.microprofile.openapi.annotations.Operation;
 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
 import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
 import org.eclipse.microprofile.openapi.annotations.tags.Tag;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.enterprise.event.Event;
 import javax.inject.Inject;
@@ -30,6 +33,7 @@
 
 @Path("/todo")
 public class TodoResource {
+    protected static final Logger LOGGER = LoggerFactory.getLogger(TodoResource.class);
 
     @Inject
     Event<TodoCreated> event;
@@ -47,6 +51,8 @@
     public Response create(@Valid TodoDto dto) {
         this.event.fire(new TodoCreated(dto));
 
+        LOGGER.info("Sent event={}", TodoCreated.class.getSimpleName());
+
         return Response.ok().build();
     }
 }
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/Todo.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/Todo.java	Sun Feb 14 20:53:27 2021 +0100
@@ -13,7 +13,7 @@
 
 import javax.validation.constraints.NotBlank;
 
-public class Todo extends TodoDto {
+public class Todo {
     private int id;
     @NotBlank
     private String title;
@@ -36,32 +36,26 @@
         this.id = id;
     }
 
-    @Override
     public String getTitle() {
         return title;
     }
 
-    @Override
     public void setTitle(String title) {
         this.title = title;
     }
 
-    @Override
     public String getDescription() {
         return description;
     }
 
-    @Override
     public void setDescription(String description) {
         this.description = description;
     }
 
-    @Override
     public Boolean getDone() {
         return done;
     }
 
-    @Override
     public void setDone(Boolean done) {
         this.done = done;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoDtoAssembler.java	Sun Feb 14 20:53:27 2021 +0100
@@ -0,0 +1,43 @@
+/**
+ * @package Quarkus-Pipes-Filters-Showcase
+ *
+ * @file Todo Dto assembler class
+ * @copyright 2020 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv2.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.domain.todo;
+
+public class TodoDtoAssembler {
+    public static TodoDto fromTodoToDto(final Todo todo) {
+        TodoDto dto = new TodoDto();
+
+        dto.setTitle(todo.getTitle());
+        dto.setDescription(todo.getDescription());
+        dto.setDone(todo.getDone());
+        dto.setStart(todo.getDueDate().getStart());
+        dto.setDue(todo.getDueDate().getDue());
+
+        return dto;
+    }
+
+    public static Todo fromDtoToTodo(final TodoDto dto) {
+        Todo todo = new Todo();
+
+        todo.setTitle(dto.getTitle());
+        todo.setDescription(dto.getDescription());
+        todo.setDone(dto.getDone());
+
+        DueDate dueDate = new DueDate();
+
+        dueDate.setStart(dto.getStart());
+        dueDate.setDue(dto.getDue());
+
+        todo.setDueDate(dueDate);
+
+        return todo;
+    }
+}
\ No newline at end of file
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoConverted.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoConverted.java	Sun Feb 14 20:53:27 2021 +0100
@@ -14,10 +14,9 @@
 import dev.unexist.showcase.todo.domain.todo.Todo;
 import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseEvent;
 import io.cloudevents.CloudEvent;
-import io.cloudevents.v1.CloudEventImpl;
 
 public class TodoConverted extends AbstractBaseEvent<Todo> {
-    public TodoConverted(CloudEventImpl<Todo> cloudEvent) {
+    public TodoConverted(CloudEvent cloudEvent) {
         super(cloudEvent);
     }
 
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoCreated.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoCreated.java	Sun Feb 14 20:53:27 2021 +0100
@@ -13,10 +13,10 @@
 
 import dev.unexist.showcase.todo.domain.todo.TodoDto;
 import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseEvent;
-import io.cloudevents.v1.CloudEventImpl;
+import io.cloudevents.CloudEvent;
 
 public class TodoCreated extends AbstractBaseEvent<TodoDto> {
-    public TodoCreated(CloudEventImpl<TodoDto> cloudEvent) {
+    public TodoCreated(CloudEvent cloudEvent) {
         super(cloudEvent);
     }
 
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoSaved.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/events/TodoSaved.java	Sun Feb 14 20:53:27 2021 +0100
@@ -13,10 +13,10 @@
 
 import dev.unexist.showcase.todo.domain.todo.Todo;
 import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseEvent;
-import io.cloudevents.v1.CloudEventImpl;
+import io.cloudevents.CloudEvent;
 
 public class TodoSaved extends AbstractBaseEvent<Todo> {
-    public TodoSaved(CloudEventImpl<Todo> cloudEvent) {
+    public TodoSaved(CloudEvent cloudEvent) {
         super(cloudEvent);
     }
 
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoConversionFilter.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoConversionFilter.java	Sun Feb 14 20:53:27 2021 +0100
@@ -10,42 +10,31 @@
 
 package dev.unexist.showcase.todo.domain.todo.filters;
 
-import dev.unexist.showcase.todo.domain.todo.DueDate;
 import dev.unexist.showcase.todo.domain.todo.Todo;
 import dev.unexist.showcase.todo.domain.todo.TodoDto;
+import dev.unexist.showcase.todo.domain.todo.TodoDtoAssembler;
 import dev.unexist.showcase.todo.domain.todo.events.TodoConverted;
 import dev.unexist.showcase.todo.domain.todo.events.TodoCreated;
 import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseFilter;
 
+import javax.enterprise.event.Observes;
 import java.util.Optional;
 
 public class TodoConversionFilter
         extends AbstractBaseFilter<TodoCreated, TodoConverted, Todo>
 {
-
     @Override
-    public void process(TodoCreated event) {
-        Todo todo = new Todo();
+    public void process(@Observes TodoCreated event) {
+        LOGGER.info("Received event={}", event.getClass().getSimpleName());
 
         Optional<TodoDto> payload = event.getPayload();
 
         if (payload.isPresent()) {
-            TodoDto dto = (TodoDto) payload.get();
-
-            todo.setTitle(dto.getTitle());
-            todo.setDescription(dto.getDescription());
-            todo.setDone(dto.getDone());
-
-            DueDate dueDate = new DueDate();
-
-            dueDate.setStart(dto.getStart());
-            dueDate.setDue(dto.getDue());
-
-            todo.setDueDate(dueDate);
+            TodoDto dto = payload.get();
 
             LOGGER.info("Received event payload={}", dto);
+
+            this.send(TodoDtoAssembler.fromDtoToTodo(dto), TodoConverted.class);
         }
-
-        this.send(todo, TodoConverted.class);
     }
 }
--- a/src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoPersistenceFilter.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/domain/todo/filters/TodoPersistenceFilter.java	Sun Feb 14 20:53:27 2021 +0100
@@ -12,12 +12,12 @@
 package dev.unexist.showcase.todo.domain.todo.filters;
 
 import dev.unexist.showcase.todo.domain.todo.Todo;
-import dev.unexist.showcase.todo.domain.todo.TodoDto;
 import dev.unexist.showcase.todo.domain.todo.TodoRepository;
 import dev.unexist.showcase.todo.domain.todo.events.TodoConverted;
 import dev.unexist.showcase.todo.domain.todo.events.TodoSaved;
 import dev.unexist.showcase.todo.infrastructure.base.AbstractBaseFilter;
 
+import javax.enterprise.event.Observes;
 import javax.inject.Inject;
 import java.util.Optional;
 
@@ -28,12 +28,15 @@
     TodoRepository repository;
 
     @Override
-    public void process(TodoConverted event) {
+    public void process(@Observes TodoConverted event) {
+        LOGGER.info("Received event={}", event.getClass().getSimpleName());
 
-        Optional<TodoDto> payload = event.getPayload();
+        Optional<Todo> payload = event.getPayload();
 
         if (payload.isPresent()) {
-            Todo todo = (Todo) payload.get();
+            Todo todo = payload.get();
+
+            LOGGER.info("Received event payload={}", todo);
 
             this.repository.add(todo);
 
--- a/src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseEvent.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseEvent.java	Sun Feb 14 20:53:27 2021 +0100
@@ -11,17 +11,17 @@
 
 package dev.unexist.showcase.todo.infrastructure.base;
 
+import io.cloudevents.CloudEvent;
 import io.cloudevents.v1.CloudEventBuilder;
-import io.cloudevents.v1.CloudEventImpl;
 
 import java.net.URI;
 import java.util.Optional;
 import java.util.UUID;
 
 public abstract class AbstractBaseEvent<DATA> {
-    private CloudEventImpl<DATA> cloudEvent;
+    private CloudEvent cloudEvent;
 
-    public AbstractBaseEvent(final CloudEventImpl<DATA> cloudEvent) {
+    public AbstractBaseEvent(final CloudEvent cloudEvent) {
         this.cloudEvent = cloudEvent;
     }
 
--- a/src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseFilter.java	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/java/dev/unexist/showcase/todo/infrastructure/base/AbstractBaseFilter.java	Sun Feb 14 20:53:27 2021 +0100
@@ -18,7 +18,6 @@
 import org.slf4j.LoggerFactory;
 
 import javax.enterprise.event.Event;
-import javax.enterprise.event.ObservesAsync;
 import javax.inject.Inject;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
@@ -31,7 +30,7 @@
     @Inject
     Event<OUT> event;
 
-    protected abstract void process(@ObservesAsync IN event);
+    protected abstract void process(IN event);
 
     protected void send(DATA data, Class<OUT> outClazz) {
         CloudEventBuilder<DATA> builder = CloudEventBuilder.builder();
--- a/src/main/resources/application.properties	Sun Feb 14 20:52:59 2021 +0100
+++ b/src/main/resources/application.properties	Sun Feb 14 20:53:27 2021 +0100
@@ -19,11 +19,10 @@
 # Smallrye Messaging
 mp.messaging.incoming.todo-in.connector=smallrye-kafka
 mp.messaging.incoming.todo-in.value.serializer=org.apache.kafka.common.serialization.StringSerializer
-mp.messaging.incoming.todo-in.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
+mp.messaging.incoming.todo-in.value.deserializer=dev.unexist.showcase.todo.infrastructure.deserializer.TodoDtoDeserializer
 
 mp.messaging.outgoing.todo-out.connector=smallrye-kafka
-mp.messaging.outgoing.todo-out.value.serializer=org.apache.kafka.common.serialization.StringSerializer
-mp.messaging.outgoing.todo-out.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
+mp.messaging.outgoing.todo-out.value.serializer=io.quarkus.kafka.client.serialization.ObjectMapperSerializer
 mp.messaging.outgoing.todo-out.topic=todo
 
 # Kafka