|
17 | 17 | import com.rabbitmq.stream.MessageHandler;
|
18 | 18 | import com.rabbitmq.stream.MessageHandler.Context;
|
19 | 19 | import com.rabbitmq.stream.OffsetSpecification;
|
| 20 | +import com.rabbitmq.stream.StreamException; |
20 | 21 | import com.rabbitmq.stream.SubscriptionListener;
|
| 22 | +import com.rabbitmq.stream.impl.Client.QueryOffsetResponse; |
21 | 23 | import com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration;
|
22 | 24 | import com.rabbitmq.stream.impl.StreamEnvironment.TrackingConsumerRegistration;
|
23 | 25 | import java.util.concurrent.atomic.AtomicBoolean;
|
|
28 | 30 | class StreamConsumer implements Consumer {
|
29 | 31 |
|
30 | 32 | private static final Logger LOGGER = LoggerFactory.getLogger(StreamConsumer.class);
|
31 |
| - |
32 |
| - private volatile Runnable closingCallback; |
33 |
| - |
34 | 33 | private final Runnable closingTrackingCallback;
|
35 |
| - |
36 | 34 | private final AtomicBoolean closed = new AtomicBoolean(false);
|
37 |
| - |
38 | 35 | private final String name;
|
39 |
| - |
40 | 36 | private final String stream;
|
41 |
| - |
42 | 37 | private final StreamEnvironment environment;
|
43 |
| - |
| 38 | + private final LongConsumer trackingCallback; |
| 39 | + private final Runnable initCallback; |
| 40 | + private volatile Runnable closingCallback; |
44 | 41 | private volatile Client trackingClient;
|
45 |
| - |
46 | 42 | private volatile Status status;
|
47 |
| - |
48 | 43 | private volatile long lastRequestedStoredOffset = 0;
|
49 | 44 |
|
50 |
| - private final LongConsumer trackingCallback; |
51 |
| - |
52 |
| - private final Runnable initCallback; |
53 |
| - |
54 | 45 | StreamConsumer(
|
55 | 46 | String stream,
|
56 | 47 | OffsetSpecification offsetSpecification,
|
@@ -195,15 +186,35 @@ void running() {
|
195 | 186 |
|
196 | 187 | long lastStoredOffset() {
|
197 | 188 | if (canTrack()) {
|
| 189 | + // the client can be null by now, so we catch any exception |
| 190 | + QueryOffsetResponse response; |
198 | 191 | try {
|
199 |
| - // the client can be null by now, but we catch the exception and return 0 |
200 |
| - // callers should know how to deal with a stored offset of 0 |
201 |
| - return this.trackingClient.queryOffset(this.name, this.stream); |
| 192 | + response = this.trackingClient.queryOffset(this.name, this.stream); |
202 | 193 | } catch (Exception e) {
|
203 |
| - return 0; |
| 194 | + throw new IllegalStateException( |
| 195 | + String.format( |
| 196 | + "Not possible to query offset for consumer %s on stream %s for now", |
| 197 | + this.name, this.stream), |
| 198 | + e); |
204 | 199 | }
|
| 200 | + if (response.isOk()) { |
| 201 | + return response.getOffset(); |
| 202 | + } else { |
| 203 | + throw new StreamException( |
| 204 | + String.format( |
| 205 | + "QueryOffset for consumer %s on stream %s returned an error", |
| 206 | + this.name, this.stream), |
| 207 | + response.getResponseCode()); |
| 208 | + } |
| 209 | + |
| 210 | + } else if (this.name == null) { |
| 211 | + throw new UnsupportedOperationException( |
| 212 | + "Not possible to query stored offset for a consumer without a name"); |
205 | 213 | } else {
|
206 |
| - return 0; |
| 214 | + throw new IllegalStateException( |
| 215 | + String.format( |
| 216 | + "Not possible to query offset for consumer %s on stream %s for now", |
| 217 | + this.name, this.stream)); |
207 | 218 | }
|
208 | 219 | }
|
209 | 220 |
|
|
0 commit comments