forked from rabbitmq/rabbitmq-stream-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamProducerBuilder.java
290 lines (243 loc) · 8.75 KB
/
StreamProducerBuilder.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2020-2023 Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
//
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.stream.impl;
import com.rabbitmq.stream.Message;
import com.rabbitmq.stream.Producer;
import com.rabbitmq.stream.ProducerBuilder;
import com.rabbitmq.stream.RoutingStrategy;
import com.rabbitmq.stream.StreamException;
import com.rabbitmq.stream.compression.Compression;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.function.Function;
import java.util.function.ToIntFunction;
class StreamProducerBuilder implements ProducerBuilder {
private final StreamEnvironment environment;
private String name;
private String stream, superStream;
private int subEntrySize = 1;
private Compression compression;
private int batchSize = 100;
private Duration batchPublishingDelay = Duration.ofMillis(100);
private int maxUnconfirmedMessages = 10_000;
private Duration confirmTimeout = Duration.ofSeconds(30);
private Duration enqueueTimeout = Duration.ofSeconds(10);
private boolean retryOnRecovery = true;
private DefaultRoutingConfiguration routingConfiguration;
private Function<Message, String> filterValueExtractor;
StreamProducerBuilder(StreamEnvironment environment) {
this.environment = environment;
}
public StreamProducerBuilder stream(String stream) {
this.stream = stream;
return this;
}
@Override
public ProducerBuilder name(String name) {
this.name = name;
return this;
}
@Override
public ProducerBuilder superStream(String superStream) {
this.superStream = superStream;
return this;
}
public StreamProducerBuilder batchSize(int batchSize) {
if (batchSize <= 0) {
throw new IllegalArgumentException("the batch size must be greater than 0");
}
this.batchSize = batchSize;
return this;
}
@Override
public ProducerBuilder subEntrySize(int subEntrySize) {
if (subEntrySize < 0) {
throw new IllegalArgumentException("the sub-entry size must be greater than 0");
}
this.subEntrySize = subEntrySize;
return this;
}
@Override
public ProducerBuilder compression(Compression compression) {
this.compression = compression;
return this;
}
public StreamProducerBuilder batchPublishingDelay(Duration batchPublishingDelay) {
this.batchPublishingDelay = batchPublishingDelay;
return this;
}
@Override
public ProducerBuilder maxUnconfirmedMessages(int maxUnconfirmedMessages) {
if (maxUnconfirmedMessages <= 0) {
throw new IllegalArgumentException(
"the maximum number of unconfirmed messages must be greater than 0");
}
this.maxUnconfirmedMessages = maxUnconfirmedMessages;
return this;
}
@Override
public ProducerBuilder confirmTimeout(Duration timeout) {
if (timeout.isNegative()) {
throw new IllegalArgumentException("the confirm timeout cannot be negative");
}
if (timeout.compareTo(Duration.ofSeconds(1)) < 0 && !timeout.isZero()) {
throw new IllegalArgumentException("the confirm timeout cannot be less than 1 second");
}
this.confirmTimeout = timeout;
return this;
}
@Override
public ProducerBuilder enqueueTimeout(Duration timeout) {
if (timeout.isNegative()) {
throw new IllegalArgumentException("the enqueue timeout cannot be negative");
}
this.enqueueTimeout = timeout;
return this;
}
@Override
public ProducerBuilder retryOnRecovery(boolean retryOnRecovery) {
this.retryOnRecovery = retryOnRecovery;
return this;
}
@Override
public ProducerBuilder filterValue(Function<Message, String> filterValueExtractor) {
this.filterValueExtractor = filterValueExtractor;
return this;
}
@Override
public RoutingConfiguration routing(Function<Message, String> routingKeyExtractor) {
this.routingConfiguration = new DefaultRoutingConfiguration(this);
this.routingConfiguration.routingKeyExtractor = routingKeyExtractor;
return this.routingConfiguration;
}
void resetRouting() {
this.routingConfiguration = null;
}
public Producer build() {
if (this.stream == null && this.superStream == null) {
throw new IllegalArgumentException("A stream must be specified");
}
if (this.stream != null && this.superStream != null) {
throw new IllegalArgumentException("Stream and superStream cannot be set at the same time");
}
if (subEntrySize == 1 && compression != null) {
throw new IllegalArgumentException(
"Sub-entry batching must be enabled to enable compression");
}
if (subEntrySize > 1 && filterValueExtractor != null) {
throw new IllegalArgumentException("Filtering is not supported with sub-entry batching");
}
if (subEntrySize > 1 && compression == null) {
compression = Compression.NONE;
}
this.environment.maybeInitializeLocator();
Producer producer;
if (this.stream != null && this.routingConfiguration != null) {
throw new IllegalArgumentException(
"A super stream must be specified when a routing configuration is set");
}
if (this.routingConfiguration != null && this.superStream == null) {
throw new IllegalArgumentException(
"A super stream must be specified when a routing configuration is set");
}
if (this.routingConfiguration == null && this.superStream != null) {
throw new IllegalArgumentException(
"A routing configuration must specified when a super stream is set");
}
if (this.stream != null) {
producer =
new StreamProducer(
name,
stream,
subEntrySize,
batchSize,
compression,
batchPublishingDelay,
maxUnconfirmedMessages,
confirmTimeout,
enqueueTimeout,
retryOnRecovery,
filterValueExtractor,
environment);
this.environment.addProducer((StreamProducer) producer);
} else {
RoutingStrategy routingStrategy = this.routingConfiguration.routingStrategy;
if (routingStrategy == null) {
if (this.routingConfiguration.hash == null) {
routingStrategy =
new RoutingKeyRoutingStrategy(this.routingConfiguration.routingKeyExtractor);
} else {
routingStrategy =
new HashRoutingStrategy(
this.routingConfiguration.routingKeyExtractor, this.routingConfiguration.hash);
}
}
producer =
new SuperStreamProducer(
this, this.name, this.superStream, routingStrategy, this.environment);
}
return producer;
}
StreamProducerBuilder duplicate() {
StreamProducerBuilder duplicate = new StreamProducerBuilder(this.environment);
for (Field field : StreamProducerBuilder.class.getDeclaredFields()) {
field.setAccessible(true);
try {
field.set(duplicate, field.get(this));
} catch (IllegalAccessException e) {
throw new StreamException("Error while duplicating stream producer builder", e);
}
}
return duplicate;
}
static final class DefaultRoutingConfiguration implements RoutingConfiguration {
private final StreamProducerBuilder producerBuilder;
private Function<Message, String> routingKeyExtractor;
private RoutingStrategy routingStrategy;
private ToIntFunction<String> hash = HashUtils.MURMUR3;
DefaultRoutingConfiguration(StreamProducerBuilder producerBuilder) {
this.producerBuilder = producerBuilder;
}
@Override
public RoutingConfiguration hash() {
if (this.hash == null) {
this.hash = HashUtils.MURMUR3;
}
this.routingStrategy = null;
return this;
}
@Override
public RoutingConfiguration hash(ToIntFunction<String> hash) {
this.hash = hash;
this.routingStrategy = null;
return this;
}
@Override
public RoutingConfiguration key() {
this.hash = null;
this.routingStrategy = null;
return this;
}
@Override
public RoutingConfiguration strategy(RoutingStrategy routingStrategy) {
this.routingStrategy = routingStrategy;
return this;
}
@Override
public ProducerBuilder producerBuilder() {
return this.producerBuilder;
}
}
}