|
14 | 14 | package com.rabbitmq.stream.impl; |
15 | 15 |
|
16 | 16 | import static com.rabbitmq.stream.impl.Utils.formatConstant; |
| 17 | +import static com.rabbitmq.stream.impl.Utils.propagateException; |
17 | 18 | import static java.util.concurrent.TimeUnit.SECONDS; |
18 | 19 |
|
19 | 20 | import com.rabbitmq.stream.Address; |
|
24 | 25 | import com.rabbitmq.stream.Environment; |
25 | 26 | import com.rabbitmq.stream.MessageHandler; |
26 | 27 | import com.rabbitmq.stream.MessageHandler.Context; |
| 28 | +import com.rabbitmq.stream.NoOffsetException; |
27 | 29 | import com.rabbitmq.stream.OffsetSpecification; |
28 | 30 | import com.rabbitmq.stream.ProducerBuilder; |
29 | 31 | import com.rabbitmq.stream.StreamCreator; |
30 | 32 | import com.rabbitmq.stream.StreamException; |
| 33 | +import com.rabbitmq.stream.StreamInfo; |
31 | 34 | import com.rabbitmq.stream.SubscriptionListener; |
32 | 35 | import com.rabbitmq.stream.compression.CompressionCodecFactory; |
33 | 36 | import com.rabbitmq.stream.impl.Client.ClientParameters; |
| 37 | +import com.rabbitmq.stream.impl.Client.StreamInfoResponse; |
34 | 38 | import com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration; |
35 | 39 | import com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration; |
36 | 40 | import com.rabbitmq.stream.impl.StreamEnvironmentBuilder.DefaultTlsConfiguration; |
|
45 | 49 | import java.net.URLDecoder; |
46 | 50 | import java.util.Collections; |
47 | 51 | import java.util.List; |
| 52 | +import java.util.Map; |
48 | 53 | import java.util.Random; |
49 | 54 | import java.util.concurrent.CopyOnWriteArrayList; |
50 | 55 | import java.util.concurrent.ExecutionException; |
|
54 | 59 | import java.util.concurrent.TimeoutException; |
55 | 60 | import java.util.concurrent.atomic.AtomicBoolean; |
56 | 61 | import java.util.concurrent.atomic.AtomicReference; |
| 62 | +import java.util.function.BiFunction; |
57 | 63 | import java.util.function.Consumer; |
58 | 64 | import java.util.function.Function; |
59 | 65 | import java.util.function.LongConsumer; |
| 66 | +import java.util.function.LongSupplier; |
60 | 67 | import java.util.function.Supplier; |
61 | 68 | import java.util.stream.Collectors; |
62 | 69 | import javax.net.ssl.SSLException; |
@@ -382,6 +389,59 @@ public void deleteStream(String stream) { |
382 | 389 | } |
383 | 390 | } |
384 | 391 |
|
| 392 | + @Override |
| 393 | + public StreamInfo queryStreamInfo(String stream) { |
| 394 | + StreamInfoResponse response = locatorOperation(client -> client.streamInfo(stream)); |
| 395 | + if (response.isOk()) { |
| 396 | + Map<String, String> info = response.getInfo(); |
| 397 | + BiFunction<String, String, LongSupplier> offsetSupplierLogic = |
| 398 | + (key, message) -> { |
| 399 | + if (!info.containsKey(key) || "-1".equals(info.get(key))) { |
| 400 | + return () -> { |
| 401 | + throw new NoOffsetException(message); |
| 402 | + }; |
| 403 | + } else { |
| 404 | + try { |
| 405 | + long firstOffset = Long.parseUnsignedLong(info.get(key)); |
| 406 | + return () -> firstOffset; |
| 407 | + } catch (NumberFormatException e) { |
| 408 | + return () -> { |
| 409 | + throw new NoOffsetException(message); |
| 410 | + }; |
| 411 | + } |
| 412 | + } |
| 413 | + }; |
| 414 | + LongSupplier firstOffsetSupplier = |
| 415 | + offsetSupplierLogic.apply("first_offset", "No first offset for stream " + stream); |
| 416 | + LongSupplier committedOffsetSupplier = |
| 417 | + offsetSupplierLogic.apply("committed_offset", "No committed offset for stream " + stream); |
| 418 | + return new DefaultStreamInfo(firstOffsetSupplier, committedOffsetSupplier); |
| 419 | + } else { |
| 420 | + throw propagateException(response.getResponseCode(), stream); |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + private static class DefaultStreamInfo implements StreamInfo { |
| 425 | + |
| 426 | + private final LongSupplier firstOffsetSupplier, committedOffsetSupplier; |
| 427 | + |
| 428 | + private DefaultStreamInfo( |
| 429 | + LongSupplier firstOffsetSupplier, LongSupplier committedOffsetSupplier) { |
| 430 | + this.firstOffsetSupplier = firstOffsetSupplier; |
| 431 | + this.committedOffsetSupplier = committedOffsetSupplier; |
| 432 | + } |
| 433 | + |
| 434 | + @Override |
| 435 | + public long firstOffset() { |
| 436 | + return firstOffsetSupplier.getAsLong(); |
| 437 | + } |
| 438 | + |
| 439 | + @Override |
| 440 | + public long committedOffset() { |
| 441 | + return committedOffsetSupplier.getAsLong(); |
| 442 | + } |
| 443 | + } |
| 444 | + |
385 | 445 | @Override |
386 | 446 | public ProducerBuilder producerBuilder() { |
387 | 447 | return new StreamProducerBuilder(this); |
|
0 commit comments