Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavBakshanskij committed Oct 22, 2024
1 parent 647514c commit a2d4ea8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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";

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -77,18 +73,45 @@ 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
* InvocationSerializer} for more information. Defaults to {@link
* 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) {
Expand Down Expand Up @@ -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
+ ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <i>TXNO_SEQUENCE</i> table in a relational database.
*/
/** Generates a sequence number based on the <i>TXNO_SEQUENCE</i> table in a relational database. */
@Builder
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class DefaultSequenceGenerator implements SequenceGenerator, Validatable {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down

0 comments on commit a2d4ea8

Please sign in to comment.