Changeset 7:f5c245dbbfa3

Added outbox handler
author unexist
date Wed, 05 May 2021 18:38:43 +0200
parents 52cdfa026a5c
children 16f5b61cc0de
files todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxEntity.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxEvent.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxRepository.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxService.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/persistence/OutboxRepositoryImpl.java
diffstat 5 files changed, 190 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxEntity.java	Wed May 05 18:38:43 2021 +0200
@@ -0,0 +1,52 @@
+/**
+ * @package Quarkus-Outbox-Showcase
+ *
+ * @file Outbox entity
+ * @copyright 2020-2021 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv3.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.infrastructure.outbox;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.util.Date;
+import java.util.UUID;
+
+import static java.util.Objects.requireNonNull;
+
+@Entity
+@Table(name = "OUTBOX")
+public class OutboxEntity {
+
+    @Id
+    @Column(name = "uuid")
+    private UUID uuid;
+
+    @Column(name = "aggregateId")
+    private Integer aggregateId;
+
+    @Column(name = "eventType")
+    private String eventType;
+
+    @Column(name = "payload")
+    private String payload;
+
+    @Column(name = "createdOn")
+    private Date createdOn;
+
+    protected OutboxEntity() {
+    }
+
+    public OutboxEntity(UUID uuid, Integer aggregateId, String eventType, String payload) {
+        this.uuid = requireNonNull(uuid);
+        this.aggregateId = requireNonNull(aggregateId);
+        this.eventType = requireNonNull(eventType);
+        this.payload = requireNonNull(payload);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxEvent.java	Wed May 05 18:38:43 2021 +0200
@@ -0,0 +1,42 @@
+/**
+ * @package Quarkus-Outbox-Showcase
+ *
+ * @file Outbox event
+ * @copyright 2020-2021 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv3.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.infrastructure.outbox;
+
+public class OutboxEvent {
+    private Integer aggregateId;
+    private String eventType;
+    private String payload;
+
+    public Integer getAggregateId() {
+        return aggregateId;
+    }
+
+    public void setAggregateId(Integer aggregateId) {
+        this.aggregateId = aggregateId;
+    }
+
+    public String getEventType() {
+        return eventType;
+    }
+
+    public void setEventType(String eventType) {
+        this.eventType = eventType;
+    }
+
+    public String getPayload() {
+        return payload;
+    }
+
+    public void setPayload(String payload) {
+        this.payload = payload;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxRepository.java	Wed May 05 18:38:43 2021 +0200
@@ -0,0 +1,26 @@
+/**
+ * @package Quarkus-Outbox-Showcase
+ *
+ * @file Outbox repository interface
+ * @copyright 2020-2021 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv3.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.infrastructure.outbox;
+
+public interface OutboxRepository {
+
+    /**
+     * Add {@link OutboxEntity} entry to list
+     *
+     * @param entity
+     *          {@link OutboxEntity} entry to add
+     * @return
+     *          Either {@code true} on success; otherwise {@code false}
+     **/
+
+    boolean add(OutboxEntity entity);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxService.java	Wed May 05 18:38:43 2021 +0200
@@ -0,0 +1,37 @@
+/**
+ * @package Quarkus-Outbox-Showcase
+ *
+ * @file Outbox domain service
+ * @copyright 2020-2021 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv3.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.infrastructure.outbox;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.event.ObservesAsync;
+import javax.enterprise.event.TransactionPhase;
+import javax.inject.Inject;
+import java.util.UUID;
+
+@ApplicationScoped
+public class OutboxService {
+
+    @Inject
+    private OutboxRepository outboxRepository;
+
+    public void handleOutboxEvent(@Observes(during=TransactionPhase.AFTER_SUCCESS) OutboxEvent event) {
+        UUID uuid = UUID.randomUUID();
+
+        OutboxEntity entity = new OutboxEntity(uuid,
+            event.getAggregateId(),
+            event.getEventType(),
+            event.getPayload());
+
+        outboxRepository.add(entity);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/persistence/OutboxRepositoryImpl.java	Wed May 05 18:38:43 2021 +0200
@@ -0,0 +1,33 @@
+/**
+ * @package Quarkus-Kubernetes-Showcase
+ *
+ * @file Outbox repository
+ * @copyright 2020-2021 Christoph Kappel <christoph@unexist.dev>
+ * @version $Id$
+ *
+ * This program can be distributed under the terms of the GNU GPLv3.
+ * See the file LICENSE for details.
+ **/
+
+package dev.unexist.showcase.todo.infrastructure.persistence;
+
+import dev.unexist.showcase.todo.infrastructure.outbox.OutboxEntity;
+import dev.unexist.showcase.todo.infrastructure.outbox.OutboxRepository;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+@ApplicationScoped
+public class OutboxRepositoryImpl implements OutboxRepository {
+
+    @Inject
+    EntityManager entityManager;
+
+    @Override
+    public boolean add(OutboxEntity outbox) {
+        this.entityManager.persist(outbox);
+
+        return true;
+    }
+}
\ No newline at end of file