@@ -37,8 +37,8 @@ public class NatsConsumerContext implements ConsumerContext, SimplifiedSubscript
37
37
private final ReentrantLock stateLock ;
38
38
private final NatsStreamContext streamCtx ;
39
39
private final boolean ordered ;
40
- private final ConsumerConfiguration originalOrderedCc ;
41
- private final String subscribeSubject ;
40
+ private final ConsumerConfiguration orderedConsumerConfigTemplate ;
41
+ private final String orderedConsumerNamePrefix ;
42
42
private final PullSubscribeOptions unorderedBindPso ;
43
43
44
44
private final AtomicReference <ConsumerInfo > cachedConsumerInfo ;
@@ -47,7 +47,7 @@ public class NatsConsumerContext implements ConsumerContext, SimplifiedSubscript
47
47
private final AtomicReference <Dispatcher > defaultDispatcher ;
48
48
private final AtomicReference <NatsMessageConsumerBase > lastConsumer ;
49
49
50
- NatsConsumerContext (NatsStreamContext sc , ConsumerInfo unorderedConsumerInfo , OrderedConsumerConfiguration orderedCc ) {
50
+ NatsConsumerContext (NatsStreamContext sc , ConsumerInfo unorderedConsumerInfo , OrderedConsumerConfiguration occ ) {
51
51
stateLock = new ReentrantLock ();
52
52
streamCtx = sc ;
53
53
cachedConsumerInfo = new AtomicReference <>();
@@ -57,23 +57,23 @@ public class NatsConsumerContext implements ConsumerContext, SimplifiedSubscript
57
57
lastConsumer = new AtomicReference <>();
58
58
if (unorderedConsumerInfo != null ) {
59
59
ordered = false ;
60
- originalOrderedCc = null ;
61
- subscribeSubject = null ;
60
+ orderedConsumerNamePrefix = null ;
61
+ orderedConsumerConfigTemplate = null ;
62
62
cachedConsumerInfo .set (unorderedConsumerInfo );
63
63
consumerName .set (unorderedConsumerInfo .getName ());
64
64
unorderedBindPso = PullSubscribeOptions .fastBind (sc .streamName , unorderedConsumerInfo .getName ());
65
65
}
66
66
else {
67
67
ordered = true ;
68
- originalOrderedCc = ConsumerConfiguration .builder ()
69
- .filterSubjects (orderedCc .getFilterSubjects ())
70
- .deliverPolicy (orderedCc .getDeliverPolicy ())
71
- .startSequence (orderedCc .getStartSequence ())
72
- .startTime (orderedCc .getStartTime ())
73
- .replayPolicy (orderedCc .getReplayPolicy ())
74
- .headersOnly (orderedCc .getHeadersOnly ())
68
+ orderedConsumerNamePrefix = occ .getConsumerNamePrefix ();
69
+ orderedConsumerConfigTemplate = ConsumerConfiguration .builder ()
70
+ .filterSubjects (occ .getFilterSubjects ())
71
+ .deliverPolicy (occ .getDeliverPolicy ())
72
+ .startSequence (occ .getStartSequence ())
73
+ .startTime (occ .getStartTime ())
74
+ .replayPolicy (occ .getReplayPolicy ())
75
+ .headersOnly (occ .getHeadersOnly ())
75
76
.build ();
76
- subscribeSubject = Validator .validateSubject (originalOrderedCc .getFilterSubject (), false );
77
77
unorderedBindPso = null ;
78
78
}
79
79
}
@@ -86,6 +86,7 @@ static class OrderedPullSubscribeOptionsBuilder extends PullSubscribeOptions.Bui
86
86
}
87
87
}
88
88
89
+ int x = 0 ;
89
90
@ Override
90
91
public NatsJetStreamPullSubscription subscribe (MessageHandler messageHandler , Dispatcher userDispatcher , PullMessageManager optionalPmm , Long optionalInactiveThreshold ) throws IOException , JetStreamApiException {
91
92
PullSubscribeOptions pso ;
@@ -94,8 +95,9 @@ public NatsJetStreamPullSubscription subscribe(MessageHandler messageHandler, Di
94
95
if (lastCon != null ) {
95
96
highestSeq .set (Math .max (highestSeq .get (), lastCon .pmm .lastStreamSeq ));
96
97
}
98
+ consumerName .set (orderedConsumerNamePrefix == null ? null : orderedConsumerNamePrefix + NUID .nextGlobalSequence ());
97
99
ConsumerConfiguration cc = streamCtx .js .consumerConfigurationForOrdered (
98
- originalOrderedCc , highestSeq .get (), null , null , optionalInactiveThreshold );
100
+ orderedConsumerConfigTemplate , highestSeq .get (), null , consumerName . get () , optionalInactiveThreshold );
99
101
pso = new OrderedPullSubscribeOptionsBuilder (streamCtx .streamName , cc ).build ();
100
102
}
101
103
else {
@@ -104,7 +106,7 @@ public NatsJetStreamPullSubscription subscribe(MessageHandler messageHandler, Di
104
106
105
107
if (messageHandler == null ) {
106
108
return (NatsJetStreamPullSubscription ) streamCtx .js .createSubscription (
107
- subscribeSubject , null , pso , null , null , null , false , optionalPmm );
109
+ null , null , pso , null , null , null , false , optionalPmm );
108
110
}
109
111
110
112
Dispatcher d = userDispatcher ;
@@ -116,7 +118,7 @@ public NatsJetStreamPullSubscription subscribe(MessageHandler messageHandler, Di
116
118
}
117
119
}
118
120
return (NatsJetStreamPullSubscription ) streamCtx .js .createSubscription (
119
- subscribeSubject , null , pso , null , (NatsDispatcher ) d , messageHandler , false , optionalPmm );
121
+ null , null , pso , null , (NatsDispatcher ) d , messageHandler , false , optionalPmm );
120
122
}
121
123
122
124
private void checkState () throws IOException {
@@ -197,7 +199,7 @@ public Message next(long maxWaitMillis) throws IOException, InterruptedException
197
199
198
200
try {
199
201
long inactiveThreshold = maxWaitMillis * 110 / 100 ; // 10% longer than the wait
200
- nmcb = new NatsMessageConsumerBase (cachedConsumerInfo .get ());
202
+ nmcb = new NatsMessageConsumerBase (consumerName . get (), cachedConsumerInfo .get ());
201
203
nmcb .initSub (subscribe (null , null , null , inactiveThreshold ));
202
204
nmcb .sub ._pull (PullRequestOptions .builder (1 )
203
205
.expiresIn (maxWaitMillis - EXPIRE_ADJUSTMENT )
@@ -258,7 +260,7 @@ public FetchConsumer fetch(FetchConsumeOptions fetchConsumeOptions) throws IOExc
258
260
stateLock .lock ();
259
261
checkState ();
260
262
Validator .required (fetchConsumeOptions , "Fetch Consume Options" );
261
- return (FetchConsumer )trackConsume (new NatsFetchConsumer (this , cachedConsumerInfo .get (), fetchConsumeOptions ));
263
+ return (FetchConsumer )trackConsume (new NatsFetchConsumer (this , consumerName . get (), cachedConsumerInfo .get (), fetchConsumeOptions ));
262
264
}
263
265
finally {
264
266
stateLock .unlock ();
@@ -282,7 +284,7 @@ public IterableConsumer iterate(ConsumeOptions consumeOptions) throws IOExceptio
282
284
stateLock .lock ();
283
285
checkState ();
284
286
Validator .required (consumeOptions , "Consume Options" );
285
- return (IterableConsumer ) trackConsume (new NatsIterableConsumer (this , cachedConsumerInfo .get (), consumeOptions ));
287
+ return (IterableConsumer ) trackConsume (new NatsIterableConsumer (this , consumerName . get (), cachedConsumerInfo .get (), consumeOptions ));
286
288
}
287
289
finally {
288
290
stateLock .unlock ();
@@ -323,7 +325,7 @@ public MessageConsumer consume(ConsumeOptions consumeOptions, Dispatcher userDis
323
325
checkState ();
324
326
Validator .required (handler , "Message Handler" );
325
327
Validator .required (consumeOptions , "Consume Options" );
326
- return trackConsume (new NatsMessageConsumer (this , cachedConsumerInfo .get (), consumeOptions , userDispatcher , handler ));
328
+ return trackConsume (new NatsMessageConsumer (this , consumerName . get (), cachedConsumerInfo .get (), consumeOptions , userDispatcher , handler ));
327
329
}
328
330
finally {
329
331
stateLock .unlock ();
0 commit comments