From a2d4ea8c253aee23253535ae173a9a3a2f2a4e35 Mon Sep 17 00:00:00 2001 From: VladislavBakshanskij Date: Tue, 22 Oct 2024 17:40:17 +0300 Subject: [PATCH] fix comments --- .../acceptance/TestRequestSerialization.java | 3 - .../transactionoutbox/DefaultPersistor.java | 123 +++++++++++++++--- .../DefaultSequenceGenerator.java | 5 +- .../transactionoutbox/SequenceGenerator.java | 4 +- 4 files changed, 111 insertions(+), 24 deletions(-) diff --git a/transactionoutbox-acceptance/src/test/java/com/gruelbox/transactionoutbox/acceptance/TestRequestSerialization.java b/transactionoutbox-acceptance/src/test/java/com/gruelbox/transactionoutbox/acceptance/TestRequestSerialization.java index f4746f2a..8b388ae9 100644 --- a/transactionoutbox-acceptance/src/test/java/com/gruelbox/transactionoutbox/acceptance/TestRequestSerialization.java +++ b/transactionoutbox-acceptance/src/test/java/com/gruelbox/transactionoutbox/acceptance/TestRequestSerialization.java @@ -30,9 +30,6 @@ final void workAlwaysSerialized() throws Exception { .persistor( DefaultPersistor.builder() .dialect(connectionDetails().dialect()) - .sequenceGenerator(DefaultSequenceGenerator.builder() - .dialect(connectionDetails().dialect()) - .build()) .serializer( DefaultInvocationSerializer.builder() .serializableTypes(Set.of(Arg.class)) diff --git a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultPersistor.java b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultPersistor.java index 1ca574bc..6a0c8ee9 100644 --- a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultPersistor.java +++ b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultPersistor.java @@ -15,10 +15,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.experimental.SuperBuilder; +import java.util.Objects; import lombok.extern.slf4j.Slf4j; /** @@ -33,10 +30,11 @@ * equally esoteric, you may prefer to implement {@link Persistor} from the ground up. */ @Slf4j -@SuperBuilder -@AllArgsConstructor(access = AccessLevel.PROTECTED) public class DefaultPersistor implements Persistor, Validatable { + private static final int DEFAULT_WRITE_LOCK_TIMEOUT_SECONDS = 2; + private static final String DEFAULT_TABLE_NAME = "TXNO_OUTBOX"; + private static final String ALL_FIELDS = "id, uniqueRequestId, invocation, topic, seq, lastAttemptTime, nextAttemptTime, attempts, blocked, processed, version"; @@ -47,8 +45,7 @@ public class DefaultPersistor implements Persistor, Validatable { * does not support skip locking. */ @SuppressWarnings("JavaDoc") - @Builder.Default - private final int writeLockTimeoutSeconds = 2; + private final int writeLockTimeoutSeconds; /** * @param dialect The database dialect to use. Required. @@ -66,8 +63,7 @@ public class DefaultPersistor implements Persistor, Validatable { * @param tableName The database table name. The default is {@code TXNO_OUTBOX}. */ @SuppressWarnings("JavaDoc") - @Builder.Default - private final String tableName = "TXNO_OUTBOX"; + private final String tableName; /** * @param migrate Set to false to disable automatic database migrations. This may be preferred if @@ -77,8 +73,7 @@ public class DefaultPersistor implements Persistor, Validatable { * the migrations. */ @SuppressWarnings("JavaDoc") - @Builder.Default - private final boolean migrate = true; + private final boolean migrate; /** * @param serializer The serializer to use for {@link Invocation}s. See {@link @@ -86,9 +81,37 @@ public class DefaultPersistor implements Persistor, Validatable { * InvocationSerializer#createDefaultJsonSerializer()} with no custom serializable classes. */ @SuppressWarnings("JavaDoc") - @Builder.Default - private final InvocationSerializer serializer = - InvocationSerializer.createDefaultJsonSerializer(); + private final InvocationSerializer serializer; + + // for backward compatibility + protected DefaultPersistor(Dialect dialect) { + this( + DEFAULT_WRITE_LOCK_TIMEOUT_SECONDS, + dialect, + DefaultSequenceGenerator.builder().dialect(dialect).build(), + DEFAULT_TABLE_NAME, + true, + InvocationSerializer.createDefaultJsonSerializer()); + } + + protected DefaultPersistor( + int writeLockTimeoutSeconds, + Dialect dialect, + SequenceGenerator sequenceGenerator, + String tableName, + boolean migrate, + InvocationSerializer serializer) { + this.writeLockTimeoutSeconds = writeLockTimeoutSeconds; + this.dialect = dialect; + this.sequenceGenerator = sequenceGenerator; + this.tableName = tableName; + this.migrate = migrate; + this.serializer = serializer; + } + + public static DefaultPersistorBuilder builder() { + return new DefaultPersistorBuilder(); + } @Override public void validate(Validator validator) { @@ -385,4 +408,74 @@ public boolean checkConnection(Transaction tx) throws SQLException { return rs.next() && (rs.getInt(1) == 1); } } + + public static class DefaultPersistorBuilder { + private Integer writeLockTimeoutSeconds; + private Dialect dialect; + private SequenceGenerator sequenceGenerator; + private String tableName; + private Boolean migrate; + private InvocationSerializer serializer; + + DefaultPersistorBuilder() {} + + public DefaultPersistorBuilder writeLockTimeoutSeconds(int writeLockTimeoutSeconds) { + this.writeLockTimeoutSeconds = writeLockTimeoutSeconds; + return this; + } + + public DefaultPersistorBuilder dialect(Dialect dialect) { + this.dialect = dialect; + return this; + } + + public DefaultPersistorBuilder sequenceGenerator(SequenceGenerator sequenceGenerator) { + this.sequenceGenerator = sequenceGenerator; + return this; + } + + public DefaultPersistorBuilder tableName(String tableName) { + this.tableName = tableName; + return this; + } + + public DefaultPersistorBuilder migrate(boolean migrate) { + this.migrate = migrate; + return this; + } + + public DefaultPersistorBuilder serializer(InvocationSerializer serializer) { + this.serializer = serializer; + return this; + } + + public DefaultPersistor build() { + + return new DefaultPersistor( + Objects.requireNonNullElse(writeLockTimeoutSeconds, 2), + this.dialect, + Objects.requireNonNullElseGet( + sequenceGenerator, () -> DefaultSequenceGenerator.builder().dialect(dialect).build()), + Objects.requireNonNullElse(tableName, DEFAULT_TABLE_NAME), + Objects.requireNonNullElse(migrate, true), + Objects.requireNonNullElseGet( + serializer, InvocationSerializer::createDefaultJsonSerializer)); + } + + public String toString() { + return "DefaultPersistor.DefaultPersistorBuilder(writeLockTimeoutSeconds$value=" + + this.writeLockTimeoutSeconds + + ", dialect=" + + this.dialect + + ", sequenceGenerator$value=" + + this.sequenceGenerator + + ", tableName$value=" + + this.tableName + + ", migrate$value=" + + this.migrate + + ", serializer$value=" + + this.serializer + + ")"; + } + } } diff --git a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultSequenceGenerator.java b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultSequenceGenerator.java index 76ff7a86..e0e60510 100644 --- a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultSequenceGenerator.java +++ b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultSequenceGenerator.java @@ -2,14 +2,11 @@ import com.gruelbox.transactionoutbox.spi.Utils; import java.sql.ResultSet; - import lombok.AccessLevel; import lombok.Builder; import lombok.RequiredArgsConstructor; -/** - * Generates a sequence number based on the TXNO_SEQUENCE table in a relational database. - */ +/** Generates a sequence number based on the TXNO_SEQUENCE table in a relational database. */ @Builder @RequiredArgsConstructor(access = AccessLevel.PROTECTED) public class DefaultSequenceGenerator implements SequenceGenerator, Validatable { diff --git a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/SequenceGenerator.java b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/SequenceGenerator.java index 455bd633..b12111c4 100644 --- a/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/SequenceGenerator.java +++ b/transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/SequenceGenerator.java @@ -1,8 +1,8 @@ package com.gruelbox.transactionoutbox; /** - * Generates sequences for a topic that is used in ordered tasks. - * For most use cases, just use {@link DefaultSequenceGenerator}. + * Generates sequences for a topic that is used in ordered tasks. For most use cases, just use + * {@link DefaultSequenceGenerator}. */ public interface SequenceGenerator { /**