1515import java .util .ArrayList ;
1616import java .util .Collection ;
1717import java .util .List ;
18- import lombok .AccessLevel ;
19- import lombok .AllArgsConstructor ;
20- import lombok .Builder ;
21- import lombok .experimental .SuperBuilder ;
18+ import java .util .Objects ;
2219import lombok .extern .slf4j .Slf4j ;
2320
2421/**
3330 * equally esoteric, you may prefer to implement {@link Persistor} from the ground up.
3431 */
3532@ Slf4j
36- @ SuperBuilder
37- @ AllArgsConstructor (access = AccessLevel .PROTECTED )
3833public class DefaultPersistor implements Persistor , Validatable {
3934
35+ private static final int DEFAULT_WRITE_LOCK_TIMEOUT_SECONDS = 2 ;
36+ private static final String DEFAULT_TABLE_NAME = "TXNO_OUTBOX" ;
37+
4038 private static final String ALL_FIELDS =
4139 "id, uniqueRequestId, invocation, topic, seq, lastAttemptTime, nextAttemptTime, attempts, blocked, processed, version" ;
4240
@@ -47,8 +45,7 @@ public class DefaultPersistor implements Persistor, Validatable {
4745 * does not support skip locking.
4846 */
4947 @ SuppressWarnings ("JavaDoc" )
50- @ Builder .Default
51- private final int writeLockTimeoutSeconds = 2 ;
48+ private final int writeLockTimeoutSeconds ;
5249
5350 /**
5451 * @param dialect The database dialect to use. Required.
@@ -66,8 +63,7 @@ public class DefaultPersistor implements Persistor, Validatable {
6663 * @param tableName The database table name. The default is {@code TXNO_OUTBOX}.
6764 */
6865 @ SuppressWarnings ("JavaDoc" )
69- @ Builder .Default
70- private final String tableName = "TXNO_OUTBOX" ;
66+ private final String tableName ;
7167
7268 /**
7369 * @param migrate Set to false to disable automatic database migrations. This may be preferred if
@@ -77,18 +73,45 @@ public class DefaultPersistor implements Persistor, Validatable {
7773 * the migrations.
7874 */
7975 @ SuppressWarnings ("JavaDoc" )
80- @ Builder .Default
81- private final boolean migrate = true ;
76+ private final boolean migrate ;
8277
8378 /**
8479 * @param serializer The serializer to use for {@link Invocation}s. See {@link
8580 * InvocationSerializer} for more information. Defaults to {@link
8681 * InvocationSerializer#createDefaultJsonSerializer()} with no custom serializable classes.
8782 */
8883 @ SuppressWarnings ("JavaDoc" )
89- @ Builder .Default
90- private final InvocationSerializer serializer =
91- InvocationSerializer .createDefaultJsonSerializer ();
84+ private final InvocationSerializer serializer ;
85+
86+ // for backward compatibility
87+ protected DefaultPersistor (Dialect dialect ) {
88+ this (
89+ DEFAULT_WRITE_LOCK_TIMEOUT_SECONDS ,
90+ dialect ,
91+ DefaultSequenceGenerator .builder ().dialect (dialect ).build (),
92+ DEFAULT_TABLE_NAME ,
93+ true ,
94+ InvocationSerializer .createDefaultJsonSerializer ());
95+ }
96+
97+ protected DefaultPersistor (
98+ int writeLockTimeoutSeconds ,
99+ Dialect dialect ,
100+ SequenceGenerator sequenceGenerator ,
101+ String tableName ,
102+ boolean migrate ,
103+ InvocationSerializer serializer ) {
104+ this .writeLockTimeoutSeconds = writeLockTimeoutSeconds ;
105+ this .dialect = dialect ;
106+ this .sequenceGenerator = sequenceGenerator ;
107+ this .tableName = tableName ;
108+ this .migrate = migrate ;
109+ this .serializer = serializer ;
110+ }
111+
112+ public static DefaultPersistorBuilder builder () {
113+ return new DefaultPersistorBuilder ();
114+ }
92115
93116 @ Override
94117 public void validate (Validator validator ) {
@@ -385,4 +408,74 @@ public boolean checkConnection(Transaction tx) throws SQLException {
385408 return rs .next () && (rs .getInt (1 ) == 1 );
386409 }
387410 }
411+
412+ public static class DefaultPersistorBuilder {
413+ private Integer writeLockTimeoutSeconds ;
414+ private Dialect dialect ;
415+ private SequenceGenerator sequenceGenerator ;
416+ private String tableName ;
417+ private Boolean migrate ;
418+ private InvocationSerializer serializer ;
419+
420+ DefaultPersistorBuilder () {}
421+
422+ public DefaultPersistorBuilder writeLockTimeoutSeconds (int writeLockTimeoutSeconds ) {
423+ this .writeLockTimeoutSeconds = writeLockTimeoutSeconds ;
424+ return this ;
425+ }
426+
427+ public DefaultPersistorBuilder dialect (Dialect dialect ) {
428+ this .dialect = dialect ;
429+ return this ;
430+ }
431+
432+ public DefaultPersistorBuilder sequenceGenerator (SequenceGenerator sequenceGenerator ) {
433+ this .sequenceGenerator = sequenceGenerator ;
434+ return this ;
435+ }
436+
437+ public DefaultPersistorBuilder tableName (String tableName ) {
438+ this .tableName = tableName ;
439+ return this ;
440+ }
441+
442+ public DefaultPersistorBuilder migrate (boolean migrate ) {
443+ this .migrate = migrate ;
444+ return this ;
445+ }
446+
447+ public DefaultPersistorBuilder serializer (InvocationSerializer serializer ) {
448+ this .serializer = serializer ;
449+ return this ;
450+ }
451+
452+ public DefaultPersistor build () {
453+
454+ return new DefaultPersistor (
455+ Objects .requireNonNullElse (writeLockTimeoutSeconds , 2 ),
456+ this .dialect ,
457+ Objects .requireNonNullElseGet (
458+ sequenceGenerator , () -> DefaultSequenceGenerator .builder ().dialect (dialect ).build ()),
459+ Objects .requireNonNullElse (tableName , DEFAULT_TABLE_NAME ),
460+ Objects .requireNonNullElse (migrate , true ),
461+ Objects .requireNonNullElseGet (
462+ serializer , InvocationSerializer ::createDefaultJsonSerializer ));
463+ }
464+
465+ public String toString () {
466+ return "DefaultPersistor.DefaultPersistorBuilder(writeLockTimeoutSeconds$value="
467+ + this .writeLockTimeoutSeconds
468+ + ", dialect="
469+ + this .dialect
470+ + ", sequenceGenerator$value="
471+ + this .sequenceGenerator
472+ + ", tableName$value="
473+ + this .tableName
474+ + ", migrate$value="
475+ + this .migrate
476+ + ", serializer$value="
477+ + this .serializer
478+ + ")" ;
479+ }
480+ }
388481}
0 commit comments