1313
1414package io .nats .client .impl ;
1515
16+ import io .nats .client .Message ;
17+
1618import java .time .Duration ;
1719import java .util .ArrayList ;
1820import java .util .concurrent .LinkedBlockingQueue ;
@@ -30,6 +32,7 @@ class MessageQueue {
3032 protected static final int STOPPED = 0 ;
3133 protected static final int RUNNING = 1 ;
3234 protected static final int DRAINING = 2 ;
35+ protected static final String POISON = "_poison" ;
3336
3437 protected final AtomicLong length ;
3538 protected final AtomicLong sizeInBytes ;
@@ -44,9 +47,10 @@ class MessageQueue {
4447 protected final Duration requestCleanupInterval ;
4548
4649 // Poison pill is a graphic, but common term for an item that breaks loops or stop something.
47- // In this class the poisonPill is used to break out of timed waits on the blocking queue.
50+ // In this class the poison pill is used to break out of timed waits on the blocking queue.
4851 // A simple == is used to check if any message in the queue is this message.
49- protected final NatsMessage poisonPill ;
52+ // /\ /\ /\ /\ which is why it is now a static. It's just a marker anyway.
53+ protected static final NatsMessage POISON_PILL = new NatsMessage (POISON , null , EMPTY_BODY );
5054
5155 MessageQueue (boolean singleReaderMode , Duration requestCleanupInterval ) {
5256 this (singleReaderMode , -1 , false , requestCleanupInterval , null );
@@ -80,9 +84,6 @@ class MessageQueue {
8084 this .offerLockMillis = requestCleanupInterval .toMillis ();
8185 this .offerTimeoutMillis = Math .max (1 , requestCleanupInterval .toMillis () * 95 / 100 );
8286
83- // The poisonPill is used to stop poll and accumulate when the queue is stopped
84- this .poisonPill = new NatsMessage ("_poison" , null , EMPTY_BODY );
85-
8687 editLock = new ReentrantLock ();
8788
8889 this .singleReaderMode = singleReaderMode ;
@@ -195,7 +196,7 @@ boolean push(NatsMessage msg, boolean internal) {
195196 */
196197 void poisonTheQueue () {
197198 try {
198- this .queue .add (this . poisonPill );
199+ this .queue .add (POISON_PILL );
199200 } catch (IllegalStateException ie ) { // queue was full, so we don't really need poison pill
200201 // ok to ignore this
201202 }
@@ -222,11 +223,11 @@ NatsMessage poll(Duration timeout) throws InterruptedException {
222223 }
223224 }
224225
225- if (msg == poisonPill ) {
226- return null ;
227- }
226+ return msg == null || isPoison (msg ) ? null : msg ;
227+ }
228228
229- return msg ;
229+ private boolean isPoison (Message msg ) {
230+ return msg == POISON_PILL ;
230231 }
231232
232233 NatsMessage pop (Duration timeout ) throws InterruptedException {
@@ -286,7 +287,7 @@ NatsMessage accumulate(long maxSize, long maxMessages, Duration timeout)
286287
287288 while (cursor != null ) {
288289 NatsMessage next = this .queue .peek ();
289- if (next != null && next != this . poisonPill ) {
290+ if (next != null && ! isPoison ( next ) ) {
290291 long s = next .getSizeInBytes ();
291292
292293 if (maxSize <0 || (size + s ) < maxSize ) { // keep going
0 commit comments