From: Kai Moritz Date: Sun, 1 Nov 2020 12:25:27 +0000 (+0100) Subject: Integrated outbox-postage instead of the copy+pasted implementation X-Git-Tag: part-2~1 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=bd0d27231000709358794cf034ab6c2b0b3db8ab;p=demos%2Fspring%2Fdata-jdbc Integrated outbox-postage instead of the copy+pasted implementation * Removed copied source-code and integrated library instead * Upgraded the used version of Java from 1.8 to 11 * Added a fully qualified name for the docker-image * Unifined naming of profiles --- diff --git a/Dockerfile b/Dockerfile index 89e2ebb..16ee25e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -FROM openjdk:8-jre-alpine +FROM openjdk:11-jre VOLUME /tmp COPY target/*.jar /opt/app.jar -ENTRYPOINT [ "/usr/bin/java", "-jar", "/opt/app.jar" ] +ENTRYPOINT [ "java", "-jar", "/opt/app.jar" ] CMD [] diff --git a/pom.xml b/pom.xml index 8430660..d666203 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,8 @@ Simple web-app example for spring-boot-data-jdbc - 1.8 + 11 + polling-outbox-2-SNAPSHOT @@ -35,6 +36,11 @@ org.springframework.boot spring-boot-starter-json + + de.juplo.kafka.outbox + outbox-postage + ${outbox-postage.version} + org.projectlombok lombok diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserController.java b/src/main/java/de/juplo/boot/data/jdbc/UserController.java index b98110e..19d0717 100644 --- a/src/main/java/de/juplo/boot/data/jdbc/UserController.java +++ b/src/main/java/de/juplo/boot/data/jdbc/UserController.java @@ -1,6 +1,6 @@ package de.juplo.boot.data.jdbc; -import de.juplo.kafka.outbox.OutboxEvent; +import de.juplo.kafka.outbox.postage.OutboxEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEventPublisher; diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java b/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java index 306a541..ff2fcc3 100644 --- a/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java +++ b/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java @@ -1,6 +1,6 @@ package de.juplo.boot.data.jdbc; -import de.juplo.kafka.outbox.OutboxEvent; +import de.juplo.kafka.outbox.postage.OutboxEvent; import java.time.ZonedDateTime; diff --git a/src/main/java/de/juplo/kafka/outbox/OutboxEvent.java b/src/main/java/de/juplo/kafka/outbox/OutboxEvent.java deleted file mode 100644 index 4ecacdb..0000000 --- a/src/main/java/de/juplo/kafka/outbox/OutboxEvent.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.juplo.kafka.outbox; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.ToString; -import org.springframework.context.ApplicationEvent; - -import java.time.ZonedDateTime; - - -@ToString -@EqualsAndHashCode -public class OutboxEvent extends ApplicationEvent -{ - @Getter - private final String key; - @Getter - private final Object value; - @Getter - private final ZonedDateTime time; - - - public OutboxEvent(Object source, String key, Object value, ZonedDateTime time) - { - super(source); - this.key = key; - this.value = value; - this.time = time; - } -} diff --git a/src/main/java/de/juplo/kafka/outbox/OutboxListener.java b/src/main/java/de/juplo/kafka/outbox/OutboxListener.java deleted file mode 100644 index fa53469..0000000 --- a/src/main/java/de/juplo/kafka/outbox/OutboxListener.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.juplo.kafka.outbox; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.AllArgsConstructor; -import org.springframework.stereotype.Component; -import org.springframework.transaction.event.TransactionPhase; -import org.springframework.transaction.event.TransactionalEventListener; - - -@Component -@AllArgsConstructor -public class OutboxListener -{ - private final OutboxRepository repository; - private final ObjectMapper mapper; - - - @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) - public void onUserEvent(OutboxEvent event) - { - try - { - repository.save( - event.getKey(), - mapper.writeValueAsString(event.getValue()), - event.getTime()); - } - catch (JsonProcessingException e) - { - throw new RuntimeException(e); - } - } -} diff --git a/src/main/java/de/juplo/kafka/outbox/OutboxRepository.java b/src/main/java/de/juplo/kafka/outbox/OutboxRepository.java deleted file mode 100644 index 006bd03..0000000 --- a/src/main/java/de/juplo/kafka/outbox/OutboxRepository.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.juplo.kafka.outbox; - -import lombok.AllArgsConstructor; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.stereotype.Repository; - -import java.sql.Timestamp; -import java.time.ZonedDateTime; - - -@Repository -@AllArgsConstructor -public class OutboxRepository -{ - private static final String SQL_UPDATE = - "INSERT INTO outbox (key, value, issued) VALUES (:key, :value, :issued)"; - - private final NamedParameterJdbcTemplate jdbcTemplate; - - - public void save(String key, String value, ZonedDateTime issued) - { - MapSqlParameterSource parameters = new MapSqlParameterSource(); - parameters.addValue("key", key); - parameters.addValue("value", value); - parameters.addValue("issued", Timestamp.from(issued.toInstant())); - jdbcTemplate.update(SQL_UPDATE, parameters); - } -}