1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -121,6 +121,42 @@ public Iterator<Message<?>> iterator() {
121
121
return getMessages ().iterator ();
122
122
}
123
123
124
+ /**
125
+ * Get the store.
126
+ * @return the store.
127
+ * @since 5.0.11
128
+ */
129
+ protected BasicMessageGroupStore getMessageGroupStore () {
130
+ return this .messageGroupStore ;
131
+ }
132
+
133
+ /**
134
+ * Get the store lock.
135
+ * @return the lock.
136
+ * @since 5.0.11
137
+ */
138
+ protected Lock getStoreLock () {
139
+ return this .storeLock ;
140
+ }
141
+
142
+ /**
143
+ * Get the not full condition.
144
+ * @return the condition.
145
+ * @since 5.0.11
146
+ */
147
+ protected Condition getMessageStoreNotFull () {
148
+ return this .messageStoreNotFull ;
149
+ }
150
+
151
+ /**
152
+ * Get the not empty condition.
153
+ * @return the condition.
154
+ * @since 5.0.11
155
+ */
156
+ protected Condition getMessageStoreNotEmpty () {
157
+ return this .messageStoreNotEmpty ;
158
+ }
159
+
124
160
@ Override
125
161
public int size () {
126
162
return this .messageGroupStore .messageGroupSize (this .groupId );
@@ -156,11 +192,11 @@ public Message<?> poll(long timeout, TimeUnit unit) throws InterruptedException
156
192
storeLock .lockInterruptibly ();
157
193
158
194
try {
159
- while (this .size () == 0 && timeoutInNanos > 0 ) {
195
+ message = doPoll ();
196
+ while (message == null && timeoutInNanos > 0 ) {
160
197
timeoutInNanos = this .messageStoreNotEmpty .awaitNanos (timeoutInNanos );
198
+ message = doPoll ();
161
199
}
162
- message = this .doPoll ();
163
-
164
200
}
165
201
finally {
166
202
storeLock .unlock ();
@@ -196,7 +232,7 @@ public int drainTo(Collection<? super Message<?>> c) {
196
232
public int drainTo (Collection <? super Message <?>> collection , int maxElements ) {
197
233
Assert .notNull (collection , "'collection' must not be null" );
198
234
int originalSize = collection .size ();
199
- ArrayList <Message <?>> list = new ArrayList <Message <?> >();
235
+ ArrayList <Message <?>> list = new ArrayList <>();
200
236
final Lock storeLock = this .storeLock ;
201
237
try {
202
238
storeLock .lockInterruptibly ();
@@ -297,7 +333,7 @@ public Message<?> take() throws InterruptedException {
297
333
while (this .size () == 0 ) {
298
334
this .messageStoreNotEmpty .await ();
299
335
}
300
- message = this . doPoll ();
336
+ message = doPoll ();
301
337
302
338
}
303
339
finally {
@@ -306,15 +342,15 @@ public Message<?> take() throws InterruptedException {
306
342
return message ;
307
343
}
308
344
309
- private Collection <Message <?>> getMessages () {
345
+ protected Collection <Message <?>> getMessages () {
310
346
return this .messageGroupStore .getMessageGroup (this .groupId ).getMessages ();
311
347
}
312
348
313
349
/**
314
350
* It is assumed that the 'storeLock' is being held by the caller, otherwise
315
351
* IllegalMonitorStateException may be thrown
316
352
*/
317
- private Message <?> doPoll () {
353
+ protected Message <?> doPoll () {
318
354
Message <?> message = this .messageGroupStore .pollMessageFromGroup (this .groupId );
319
355
this .messageStoreNotFull .signal ();
320
356
return message ;
@@ -323,8 +359,9 @@ private Message<?> doPoll() {
323
359
/**
324
360
* It is assumed that the 'storeLock' is being held by the caller, otherwise
325
361
* IllegalMonitorStateException may be thrown
362
+ * @param message the message to offer.
326
363
*/
327
- private boolean doOffer (Message <?> message ) {
364
+ protected boolean doOffer (Message <?> message ) {
328
365
boolean offered = false ;
329
366
if (this .capacity == Integer .MAX_VALUE || this .size () < this .capacity ) {
330
367
this .messageGroupStore .addMessageToGroup (this .groupId , message );
@@ -333,4 +370,5 @@ private boolean doOffer(Message<?> message) {
333
370
}
334
371
return offered ;
335
372
}
373
+
336
374
}
0 commit comments