16
16
import com .rabbitmq .stream .Message ;
17
17
import com .rabbitmq .stream .Producer ;
18
18
import com .rabbitmq .stream .ProducerBuilder ;
19
+ import com .rabbitmq .stream .RoutingStrategy ;
19
20
import com .rabbitmq .stream .StreamException ;
20
21
import com .rabbitmq .stream .compression .Compression ;
21
22
import java .lang .reflect .Field ;
@@ -45,11 +46,7 @@ class StreamProducerBuilder implements ProducerBuilder {
45
46
46
47
private Duration enqueueTimeout = Duration .ofSeconds (10 );
47
48
48
- private Function <Message , String > routingKeyExtractor ;
49
-
50
- private RoutingType routingType ;
51
-
52
- private ToIntFunction <String > hash = HashUtils .MURMUR3 ;
49
+ private DefaultRoutingConfiguration routingConfiguration ;
53
50
54
51
StreamProducerBuilder (StreamEnvironment environment ) {
55
52
this .environment = environment ;
@@ -126,32 +123,14 @@ public ProducerBuilder enqueueTimeout(Duration timeout) {
126
123
}
127
124
128
125
@ Override
129
- public ProducerBuilder routing (
130
- Function <Message , String > routingKeyExtractor , RoutingType routingType ) {
131
- return this .routing (routingKeyExtractor , routingType , HashUtils .MURMUR3 );
132
- }
133
-
134
- @ Override
135
- public ProducerBuilder routing (
136
- Function <Message , String > routingKeyExtractor ,
137
- RoutingType routingType ,
138
- ToIntFunction <String > hash ) {
139
- if (routingKeyExtractor == null || routingType == null ) {
140
- throw new IllegalArgumentException (
141
- "both routing key extractor and routing type must be non-null" );
142
- }
143
- this .routingKeyExtractor = routingKeyExtractor ;
144
- this .routingType = routingType ;
145
- if (hash != null ) {
146
- this .hash = hash ;
147
- }
148
- return this ;
126
+ public RoutingConfiguration routing (Function <Message , String > routingKeyExtractor ) {
127
+ this .routingConfiguration = new DefaultRoutingConfiguration (this );
128
+ this .routingConfiguration .routingKeyExtractor = routingKeyExtractor ;
129
+ return this .routingConfiguration ;
149
130
}
150
131
151
132
void resetRouting () {
152
- this .routingKeyExtractor = null ;
153
- this .routingType = null ;
154
- this .hash = null ;
133
+ this .routingConfiguration = null ;
155
134
}
156
135
157
136
public Producer build () {
@@ -164,7 +143,7 @@ public Producer build() {
164
143
}
165
144
this .environment .maybeInitializeLocator ();
166
145
Producer producer ;
167
- if (this .routingKeyExtractor == null ) {
146
+ if (this .routingConfiguration == null ) {
168
147
producer =
169
148
new StreamProducer (
170
149
name ,
@@ -179,14 +158,17 @@ public Producer build() {
179
158
environment );
180
159
this .environment .addProducer ((StreamProducer ) producer );
181
160
} else {
182
- // FIXME propagate compression to super stream producer
183
- ToIntFunction <String > hashFunction = this .hash == null ? HashUtils .MURMUR3 : this .hash ;
184
- RoutingStrategy routingStrategy =
185
- this .routingType == RoutingType .HASH
186
- ? new HashRoutingStrategy (
187
- this .stream , this .routingKeyExtractor , this .environment , hashFunction )
188
- : new RoutingKeyRoutingStrategy (
189
- this .stream , this .routingKeyExtractor , this .environment );
161
+ RoutingStrategy routingStrategy = this .routingConfiguration .routingStrategy ;
162
+ if (routingStrategy == null ) {
163
+ if (this .routingConfiguration .hash == null ) {
164
+ routingStrategy =
165
+ new RoutingKeyRoutingStrategy (this .routingConfiguration .routingKeyExtractor );
166
+ } else {
167
+ routingStrategy =
168
+ new HashRoutingStrategy (
169
+ this .routingConfiguration .routingKeyExtractor , this .routingConfiguration .hash );
170
+ }
171
+ }
190
172
producer =
191
173
new SuperStreamProducer (this , this .name , this .stream , routingStrategy , this .environment );
192
174
}
@@ -205,4 +187,53 @@ StreamProducerBuilder duplicate() {
205
187
}
206
188
return duplicate ;
207
189
}
190
+
191
+ static final class DefaultRoutingConfiguration implements RoutingConfiguration {
192
+
193
+ private final StreamProducerBuilder producerBuilder ;
194
+
195
+ private Function <Message , String > routingKeyExtractor ;
196
+
197
+ private RoutingStrategy routingStrategy ;
198
+
199
+ private ToIntFunction <String > hash = HashUtils .MURMUR3 ;
200
+
201
+ DefaultRoutingConfiguration (StreamProducerBuilder producerBuilder ) {
202
+ this .producerBuilder = producerBuilder ;
203
+ }
204
+
205
+ @ Override
206
+ public RoutingConfiguration hash () {
207
+ if (this .hash == null ) {
208
+ this .hash = HashUtils .MURMUR3 ;
209
+ }
210
+ this .routingStrategy = null ;
211
+ return this ;
212
+ }
213
+
214
+ @ Override
215
+ public RoutingConfiguration hash (ToIntFunction <String > hash ) {
216
+ this .hash = hash ;
217
+ this .routingStrategy = null ;
218
+ return this ;
219
+ }
220
+
221
+ @ Override
222
+ public RoutingConfiguration key () {
223
+ this .hash = null ;
224
+ this .routingStrategy = null ;
225
+ return this ;
226
+ }
227
+
228
+ @ Override
229
+ public RoutingConfiguration strategy (RoutingStrategy routingStrategy ) {
230
+ this .routingStrategy = routingStrategy ;
231
+ return this ;
232
+ }
233
+
234
+ @ Override
235
+ public ProducerBuilder producerBuilder () {
236
+ return this .producerBuilder ;
237
+ }
238
+ }
208
239
}
0 commit comments