|
31 | 31 | import com.rabbitmq.stream.impl.Client.StreamMetadata;
|
32 | 32 | import io.netty.channel.EventLoopGroup;
|
33 | 33 | import io.netty.channel.nio.NioEventLoopGroup;
|
| 34 | +import io.vavr.Tuple; |
| 35 | +import io.vavr.Tuple2; |
34 | 36 | import java.io.BufferedReader;
|
35 | 37 | import java.io.IOException;
|
36 | 38 | import java.io.InputStream;
|
|
45 | 47 | import java.lang.reflect.Method;
|
46 | 48 | import java.nio.charset.StandardCharsets;
|
47 | 49 | import java.time.Duration;
|
| 50 | +import java.util.ArrayList; |
48 | 51 | import java.util.Collection;
|
49 | 52 | import java.util.Collections;
|
50 | 53 | import java.util.List;
|
@@ -231,17 +234,28 @@ static void declareSuperStreamTopology(Connection connection, String superStream
|
231 | 234 | IntStream.range(0, partitions).mapToObj(String::valueOf).toArray(String[]::new));
|
232 | 235 | }
|
233 | 236 |
|
234 |
| - static void declareSuperStreamTopology( |
235 |
| - Connection connection, String superStream, String... routingKeys) throws Exception { |
| 237 | + static void declareSuperStreamTopology(Connection connection, String superStream, String... rks) |
| 238 | + throws Exception { |
236 | 239 | try (Channel ch = connection.createChannel()) {
|
237 | 240 | ch.exchangeDeclare(superStream, BuiltinExchangeType.DIRECT, true);
|
238 |
| - for (String routingKey : routingKeys) { |
| 241 | + |
| 242 | + List<Tuple2<String, Integer>> bindings = new ArrayList<>(rks.length); |
| 243 | + for (int i = 0; i < rks.length; i++) { |
| 244 | + bindings.add(Tuple.of(rks[i], i)); |
| 245 | + } |
| 246 | + // shuffle the order to make sure we get in the correct order from the server |
| 247 | + Collections.shuffle(bindings); |
| 248 | + |
| 249 | + for (Tuple2<String, Integer> binding : bindings) { |
| 250 | + String routingKey = binding._1(); |
239 | 251 | String partitionName = superStream + "-" + routingKey;
|
240 | 252 | ch.queueDeclare(
|
241 | 253 | partitionName, true, false, false, Collections.singletonMap("x-queue-type", "stream"));
|
242 |
| - // TODO consider adding some arguments to the bindings |
243 |
| - // can be useful to identify a partition, e.g. partition number |
244 |
| - ch.queueBind(partitionName, superStream, routingKey); |
| 254 | + ch.queueBind( |
| 255 | + partitionName, |
| 256 | + superStream, |
| 257 | + routingKey, |
| 258 | + Collections.singletonMap("x-stream-partition-order", binding._2())); |
245 | 259 | }
|
246 | 260 | }
|
247 | 261 | }
|
|
0 commit comments