-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPostgresKafkaEventProcessor.java
More file actions
160 lines (144 loc) · 8.53 KB
/
PostgresKafkaEventProcessor.java
File metadata and controls
160 lines (144 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fr.maif.eventsourcing;
import fr.maif.eventsourcing.format.JacksonEventFormat;
import fr.maif.eventsourcing.format.JacksonSimpleFormat;
import fr.maif.reactor.eventsourcing.DefaultAggregateStore;
import fr.maif.eventsourcing.impl.PostgresEventStore;
import fr.maif.reactor.eventsourcing.ReactorKafkaEventPublisher;
import fr.maif.eventsourcing.impl.TableNames;
import io.vavr.collection.List;
import io.vavr.control.Option;
import reactor.kafka.sender.SenderOptions;
import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.util.concurrent.Executor;
import static fr.maif.eventsourcing.EventStore.ConcurrentReplayStrategy.SKIP;
public class PostgresKafkaEventProcessor<Error, S extends State<S>, C extends Command<Meta, Context>, E extends Event, Message, Meta, Context> extends EventProcessorImpl<Error, S, C, E, Connection, Message, Meta, Context> implements Closeable {
private final PostgresKafkaEventProcessorConfig<Error, S, C, E, Message, Meta, Context> config;
public static PostgresKafkaEventProcessorBuilder.BuilderWithPool withDataSource(DataSource dataSource) {
return new PostgresKafkaEventProcessorBuilder.BuilderWithPool(dataSource);
}
public PostgresKafkaEventProcessor(PostgresKafkaEventProcessorConfig<Error, S, C, E, Message, Meta, Context> config) {
super(
config.eventStore,
config.transactionManager,
config.aggregateStore,
config.commandHandler,
config.eventHandler,
config.projections
);
this.config = config;
config.eventPublisher.start(config.eventStore, Option.of(config.concurrentReplayStrategy).getOrElse(SKIP));
}
@Override
public void close() throws IOException {
this.config.eventPublisher.close();
}
public static class PostgresKafkaEventProcessorConfig<Error, S extends State<S>, C extends Command<Meta, Context>, E extends Event, Message, Meta, Context> {
public final EventStore.ConcurrentReplayStrategy concurrentReplayStrategy;
public final PostgresEventStore<E, Meta, Context> eventStore;
public final TransactionManager<Connection> transactionManager;
public final AggregateStore<S, String, Connection> aggregateStore;
public final CommandHandler<Error, S, C, E, Message, Connection> commandHandler;
public final EventHandler<S, E> eventHandler;
public final List<Projection<Connection, E, Meta, Context>> projections;
public final EventPublisher<E, Meta, Context> eventPublisher;
public PostgresKafkaEventProcessorConfig(
EventStore.ConcurrentReplayStrategy concurrentReplayStrategy,
TableNames tableNames,
DataSource dataSource,
String topic,
Executor executionContext,
SenderOptions<String, EventEnvelope<E, Meta, Context>> producerSettings,
TransactionManager<Connection> transactionManager,
AggregateStore<S, String, Connection> aggregateStore, CommandHandler<Error, S, C, E, Message, Connection> commandHandler,
EventHandler<S, E> eventHandler,
List<Projection<Connection, E, Meta, Context>> projections,
JacksonEventFormat<?, E> eventFormat, JacksonSimpleFormat<Meta> metaFormat,
JacksonSimpleFormat<Context> contextFormat,
Integer eventsBufferSize) {
this.concurrentReplayStrategy = concurrentReplayStrategy;
this.transactionManager = transactionManager;
this.eventPublisher = new ReactorKafkaEventPublisher<>(producerSettings, topic, eventsBufferSize);
this.eventStore = new PostgresEventStore<>(
eventPublisher,
dataSource,
executionContext,
tableNames,
eventFormat,
metaFormat,
contextFormat
);
this.aggregateStore = aggregateStore == null ? new DefaultAggregateStore<>(new AutoSnapshotingStrategy.NoOpSnapshotingStrategy(), this.eventStore, eventHandler, transactionManager) : aggregateStore;
this.commandHandler = commandHandler;
this.eventHandler = eventHandler;
this.projections = projections;
}
public PostgresKafkaEventProcessorConfig(
TableNames tableNames,
DataSource dataSource,
String topic,
SenderOptions<String, EventEnvelope<E, Meta, Context>> producerSettings,
Executor executionContext,
TransactionManager<Connection> transactionManager,
AggregateStore<S, String, Connection> aggregateStore,
CommandHandler<Error, S, C, E, Message, Connection> commandHandler,
EventHandler<S, E> eventHandler,
List<Projection<Connection, E, Meta, Context>> projections,
JacksonEventFormat<?, E> eventFormat,
JacksonSimpleFormat<Meta> metaFormat,
JacksonSimpleFormat<Context> contextFormat, EventStore.ConcurrentReplayStrategy concurrentReplayStrategy) {
this(tableNames, dataSource, topic, producerSettings, executionContext, transactionManager, commandHandler, eventHandler, projections, eventFormat, metaFormat, contextFormat, null, concurrentReplayStrategy);
}
public PostgresKafkaEventProcessorConfig(
TableNames tableNames,
DataSource dataSource,
String topic,
SenderOptions<String, EventEnvelope<E, Meta, Context>> producerSettings,
Executor executionContext,
TransactionManager<Connection> transactionManager,
CommandHandler<Error, S, C, E, Message, Connection> commandHandler, EventHandler<S, E> eventHandler,
List<Projection<Connection, E, Meta, Context>> projections,
JacksonEventFormat<?, E> eventFormat,
JacksonSimpleFormat<Meta> metaFormat,
JacksonSimpleFormat<Context> contextFormat,
Integer eventsBufferSize, EventStore.ConcurrentReplayStrategy concurrentReplayStrategy) {
this(concurrentReplayStrategy, tableNames, dataSource, topic, executionContext, producerSettings, transactionManager, null, commandHandler, eventHandler, projections, eventFormat, metaFormat, contextFormat, eventsBufferSize);
}
public PostgresKafkaEventProcessorConfig(
EventStore.ConcurrentReplayStrategy concurrentReplayStrategy, PostgresEventStore<E, Meta, Context> eventStore,
TransactionManager<Connection> transactionManager,
AggregateStore<S, String, Connection> aggregateStore,
CommandHandler<Error, S, C, E, Message, Connection> commandHandler,
EventHandler<S, E> eventHandler,
List<Projection<Connection, E, Meta, Context>> projections,
EventPublisher<E, Meta, Context> eventPublisher) {
this.concurrentReplayStrategy = concurrentReplayStrategy;
this.eventStore = eventStore;
this.transactionManager = transactionManager;
this.aggregateStore = aggregateStore;
this.commandHandler = commandHandler;
this.eventHandler = eventHandler;
this.projections = projections;
this.eventPublisher = eventPublisher;
}
public PostgresKafkaEventProcessorConfig(
EventStore.ConcurrentReplayStrategy concurrentReplayStrategy,
PostgresEventStore<E, Meta, Context> eventStore,
TransactionManager<Connection> transactionManager,
CommandHandler<Error, S, C, E, Message, Connection> commandHandler,
EventHandler<S, E> eventHandler,
List<Projection<Connection, E, Meta, Context>> projections,
ReactorKafkaEventPublisher<E, Meta, Context> eventPublisher) {
this.concurrentReplayStrategy = concurrentReplayStrategy;
this.eventStore = eventStore;
this.transactionManager = transactionManager;
this.commandHandler = commandHandler;
this.eventHandler = eventHandler;
this.projections = projections;
this.eventPublisher = eventPublisher;
this.aggregateStore = new DefaultAggregateStore<>(new AutoSnapshotingStrategy.NoOpSnapshotingStrategy(), this.eventStore, eventHandler, transactionManager);
}
}
}