/
QueueImpl.java
3325 lines (2748 loc) · 90.6 KB
/
QueueImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.core.server.impl;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.Message;
import org.hornetq.api.core.SimpleString;
import org.hornetq.api.core.management.CoreNotificationType;
import org.hornetq.api.core.management.ManagementHelper;
import org.hornetq.core.filter.Filter;
import org.hornetq.core.paging.cursor.PageSubscription;
import org.hornetq.core.paging.cursor.PagedReference;
import org.hornetq.core.persistence.StorageManager;
import org.hornetq.core.postoffice.Bindings;
import org.hornetq.core.postoffice.DuplicateIDCache;
import org.hornetq.core.postoffice.PostOffice;
import org.hornetq.core.postoffice.impl.PostOfficeImpl;
import org.hornetq.core.remoting.server.RemotingService;
import org.hornetq.core.server.Consumer;
import org.hornetq.core.server.HandleStatus;
import org.hornetq.core.server.HornetQMessageBundle;
import org.hornetq.core.server.HornetQServerLogger;
import org.hornetq.core.server.MessageReference;
import org.hornetq.core.server.Queue;
import org.hornetq.core.server.RoutingContext;
import org.hornetq.core.server.ScheduledDeliveryHandler;
import org.hornetq.core.server.ServerMessage;
import org.hornetq.core.server.cluster.impl.Redistributor;
import org.hornetq.core.server.management.ManagementService;
import org.hornetq.core.server.management.Notification;
import org.hornetq.core.settings.HierarchicalRepository;
import org.hornetq.core.settings.HierarchicalRepositoryChangeListener;
import org.hornetq.core.settings.impl.AddressSettings;
import org.hornetq.core.settings.impl.SlowConsumerPolicy;
import org.hornetq.core.transaction.Transaction;
import org.hornetq.core.transaction.TransactionOperationAbstract;
import org.hornetq.core.transaction.TransactionPropertyIndexes;
import org.hornetq.core.transaction.impl.BindingsTransactionImpl;
import org.hornetq.core.transaction.impl.TransactionImpl;
import org.hornetq.spi.core.protocol.RemotingConnection;
import org.hornetq.utils.ConcurrentHashSet;
import org.hornetq.utils.FutureLatch;
import org.hornetq.utils.LinkedListIterator;
import org.hornetq.utils.PriorityLinkedList;
import org.hornetq.utils.PriorityLinkedListImpl;
import org.hornetq.utils.ReusableLatch;
import org.hornetq.utils.TypedProperties;
/**
* Implementation of a Queue
* <p/>
* Completely non blocking between adding to queue and delivering to consumers.
*
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
* @author <a href="ataylor@redhat.com">Andy Taylor</a>
* @author <a href="jmesnil@redhat.com">Jeff Mesnil</a>
* @author <a href="clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class QueueImpl implements Queue
{
private static final boolean isTrace = HornetQServerLogger.LOGGER.isTraceEnabled();
public static final int REDISTRIBUTOR_BATCH_SIZE = 100;
public static final int NUM_PRIORITIES = 10;
public static final int MAX_DELIVERIES_IN_LOOP = 1000;
public static final int CHECK_QUEUE_SIZE_PERIOD = 100;
/**
* If The system gets slow for any reason, this is the maximum time an Delivery or
* or depage executor should be hanging on
*/
public static final int DELIVERY_TIMEOUT = 1000;
private static final int FLUSH_TIMEOUT = 10000;
private final long id;
private final SimpleString name;
private volatile Filter filter;
private final boolean durable;
private final boolean temporary;
private final PostOffice postOffice;
private volatile boolean queueDestroyed = false;
private final PageSubscription pageSubscription;
private final LinkedListIterator<PagedReference> pageIterator;
// Messages will first enter intermediateMessageReferences
// Before they are added to messageReferences
// This is to avoid locking the queue on the producer
private final ConcurrentLinkedQueue<MessageReference> intermediateMessageReferences = new ConcurrentLinkedQueue<MessageReference>();
// This is where messages are stored
private final PriorityLinkedList<MessageReference> messageReferences = new PriorityLinkedListImpl<MessageReference>(QueueImpl.NUM_PRIORITIES);
// The quantity of pagedReferences on messageReferences priority list
private final AtomicInteger pagedReferences = new AtomicInteger(0);
// The estimate of memory being consumed by this queue. Used to calculate instances of messages to depage
private final AtomicInteger queueMemorySize = new AtomicInteger(0);
// used to control if we should recalculate certain positions inside deliverAsync
private volatile boolean consumersChanged = true;
private final List<ConsumerHolder> consumerList = new CopyOnWriteArrayList<ConsumerHolder>();
private final ScheduledDeliveryHandler scheduledDeliveryHandler;
private volatile long messagesAdded;
protected final AtomicInteger deliveringCount = new AtomicInteger(0);
private boolean paused;
private static final int MAX_SCHEDULED_RUNNERS = 2;
// We don't ever need more than two DeliverRunner on the executor's list
// that is getting the worse scenario possible when one runner is almost finishing before the second started
// for that we keep a counter of scheduled instances
private final AtomicInteger scheduledRunners = new AtomicInteger(0);
private final Runnable deliverRunner = new DeliverRunner();
private volatile boolean depagePending = false;
private final StorageManager storageManager;
private final HierarchicalRepository<AddressSettings> addressSettingsRepository;
private final ScheduledExecutorService scheduledExecutor;
private final SimpleString address;
private Redistributor redistributor;
private final Set<ScheduledFuture<?>> futures = new ConcurrentHashSet<ScheduledFuture<?>>();
private ScheduledFuture<?> redistributorFuture;
private ScheduledFuture<?> checkQueueSizeFuture;
// We cache the consumers here since we don't want to include the redistributor
private final Set<Consumer> consumerSet = new HashSet<Consumer>();
private final Map<SimpleString, Consumer> groups = new HashMap<SimpleString, Consumer>();
private volatile SimpleString expiryAddress;
private int pos;
private final Executor executor;
private boolean internalQueue;
private volatile long lastDirectDeliveryCheck = 0;
private volatile boolean directDeliver = true;
private AddressSettingsRepositoryListener addressSettingsRepositoryListener;
private final ExpiryScanner expiryScanner = new ExpiryScanner();
private final ReusableLatch deliveriesInTransit = new ReusableLatch(0);
private AtomicLong queueRateCheckTime = new AtomicLong(System.currentTimeMillis());
private AtomicLong messagesAddedSnapshot = new AtomicLong(0);
private ScheduledFuture slowConsumerReaperFuture;
private SlowConsumerReaperRunnable slowConsumerReaperRunnable;
private long messagesAcknowledged;
/**
* This is to avoid multi-thread races on calculating direct delivery,
* to guarantee ordering will be always be correct
*/
private final Object directDeliveryGuard = new Object();
/**
* For testing only
* */
public List<SimpleString> getGroupsUsed()
{
final CountDownLatch flush = new CountDownLatch(1);
executor.execute(new Runnable()
{
public void run()
{
flush.countDown();
}
});
try
{
flush.await(10, TimeUnit.SECONDS);
}
catch (Exception ignored)
{
}
synchronized (this)
{
ArrayList<SimpleString> groupsUsed = new ArrayList<SimpleString>();
groupsUsed.addAll(groups.keySet());
return groupsUsed;
}
}
public String debug()
{
StringWriter str = new StringWriter();
PrintWriter out = new PrintWriter(str);
out.println("queueMemorySize=" + queueMemorySize);
for (ConsumerHolder holder : consumerList)
{
out.println("consumer: " + holder.consumer.debug());
}
for (MessageReference reference : intermediateMessageReferences)
{
out.print("Intermediate reference:" + reference);
}
if (intermediateMessageReferences.isEmpty())
{
out.println("No intermediate references");
}
boolean foundRef = false;
synchronized (this)
{
Iterator<MessageReference> iter = messageReferences.iterator();
while (iter.hasNext())
{
foundRef = true;
out.println("reference = " + iter.next());
}
}
if (!foundRef)
{
out.println("No permanent references on queue");
}
System.out.println(str.toString());
return str.toString();
}
public QueueImpl(final long id,
final SimpleString address,
final SimpleString name,
final Filter filter,
final boolean durable,
final boolean temporary,
final ScheduledExecutorService scheduledExecutor,
final PostOffice postOffice,
final StorageManager storageManager,
final HierarchicalRepository<AddressSettings> addressSettingsRepository,
final Executor executor)
{
this(id,
address,
name,
filter,
null,
durable,
temporary,
scheduledExecutor,
postOffice,
storageManager,
addressSettingsRepository,
executor);
}
public QueueImpl(final long id,
final SimpleString address,
final SimpleString name,
final Filter filter,
final PageSubscription pageSubscription,
final boolean durable,
final boolean temporary,
final ScheduledExecutorService scheduledExecutor,
final PostOffice postOffice,
final StorageManager storageManager,
final HierarchicalRepository<AddressSettings> addressSettingsRepository,
final Executor executor)
{
this.id = id;
this.address = address;
this.name = name;
this.filter = filter;
this.pageSubscription = pageSubscription;
this.durable = durable;
this.temporary = temporary;
this.postOffice = postOffice;
this.storageManager = storageManager;
this.addressSettingsRepository = addressSettingsRepository;
this.scheduledExecutor = scheduledExecutor;
scheduledDeliveryHandler = new ScheduledDeliveryHandlerImpl(scheduledExecutor);
if (addressSettingsRepository != null)
{
addressSettingsRepositoryListener = new AddressSettingsRepositoryListener();
addressSettingsRepository.registerListener(addressSettingsRepositoryListener);
}
else
{
expiryAddress = null;
}
if (pageSubscription != null)
{
pageSubscription.setQueue(this);
this.pageIterator = pageSubscription.iterator();
}
else
{
this.pageIterator = null;
}
this.executor = executor;
}
// Bindable implementation -------------------------------------------------------------------------------------
public SimpleString getRoutingName()
{
return name;
}
public SimpleString getUniqueName()
{
return name;
}
public boolean isExclusive()
{
return false;
}
public void route(final ServerMessage message, final RoutingContext context) throws Exception
{
context.addQueue(address, this);
}
// Queue implementation ----------------------------------------------------------------------------------------
public boolean isDurable()
{
return durable;
}
public boolean isTemporary()
{
return temporary;
}
public SimpleString getName()
{
return name;
}
public SimpleString getAddress()
{
return address;
}
public long getID()
{
return id;
}
public PageSubscription getPageSubscription()
{
return pageSubscription;
}
public Filter getFilter()
{
return filter;
}
public void unproposed(final SimpleString groupID)
{
if (groupID.toString().endsWith("." + this.getName()))
{
// this means this unproposed belongs to this routing, so we will
// remove this group
// This is removing the name and a . added, giving us the original groupID used
// this is because a groupID is stored per queue, and only this queue is expiring at this point
final SimpleString groupIDToRemove = (SimpleString)groupID.subSequence(0, groupID.length() - getName().length() - 1);
// using an executor so we don't want to hold anyone just because of this
getExecutor().execute(new Runnable()
{
public void run()
{
synchronized (QueueImpl.this)
{
if (groups.remove(groupIDToRemove) != null)
{
HornetQServerLogger.LOGGER.debug("Removing group after unproposal " + groupID + " from queue " + QueueImpl.this);
}
else
{
HornetQServerLogger.LOGGER.debug("Couldn't remove Removing group " + groupIDToRemove + " after unproposal on queue " + QueueImpl.this);
}
}
}
});
}
}
/* Called when a message is cancelled back into the queue */
public synchronized void addHead(final MessageReference ref)
{
flushDeliveriesInTransit();
if (scheduledDeliveryHandler.checkAndSchedule(ref, false))
{
return;
}
internalAddHead(ref);
directDeliver = false;
}
/* Called when a message is cancelled back into the queue */
public synchronized void addHead(final List<MessageReference> refs)
{
flushDeliveriesInTransit();
for (MessageReference ref : refs)
{
addHead(ref);
}
resetAllIterators();
deliverAsync();
}
public synchronized void reload(final MessageReference ref)
{
queueMemorySize.addAndGet(ref.getMessageMemoryEstimate());
if (!scheduledDeliveryHandler.checkAndSchedule(ref, true))
{
internalAddTail(ref);
}
directDeliver = false;
if (!ref.isPaged())
{
messagesAdded++;
}
}
public void addTail(final MessageReference ref)
{
addTail(ref, false);
}
public void addTail(final MessageReference ref, final boolean direct)
{
if (scheduledDeliveryHandler.checkAndSchedule(ref, true))
{
synchronized (this)
{
if (!ref.isPaged())
{
messagesAdded++;
}
}
return;
}
synchronized (directDeliveryGuard)
{
// The checkDirect flag is periodically set to true, if the delivery is specified as direct then this causes the
// directDeliver flag to be re-computed resulting in direct delivery if the queue is empty
// We don't recompute it on every delivery since executing isEmpty is expensive for a ConcurrentQueue
if (!directDeliver &&
direct &&
System.currentTimeMillis() - lastDirectDeliveryCheck > CHECK_QUEUE_SIZE_PERIOD)
{
lastDirectDeliveryCheck = System.currentTimeMillis();
if (intermediateMessageReferences.isEmpty() &&
messageReferences.isEmpty() &&
!pageIterator.hasNext() &&
!pageSubscription.isPaging())
{
// We must block on the executor to ensure any async deliveries have completed or we might get out of order
// deliveries
if (flushExecutor() && flushDeliveriesInTransit())
{
// Go into direct delivery mode
directDeliver = true;
}
}
}
}
if (direct && directDeliver && deliveriesInTransit.getCount() == 0 && deliverDirect(ref))
{
return;
}
// We only add queueMemorySize if not being delivered directly
queueMemorySize.addAndGet(ref.getMessageMemoryEstimate());
intermediateMessageReferences.add(ref);
directDeliver = false;
// Delivery async will both poll for intermediate reference and deliver to clients
deliverAsync();
}
/**
* This will wait for any pending deliveries to finish
*/
private boolean flushDeliveriesInTransit()
{
try
{
if (deliveriesInTransit.await(DELIVERY_TIMEOUT))
{
return true;
}
else
{
HornetQServerLogger.LOGGER.timeoutFlushInTransit(getName().toString(), getAddress().toString());
return false;
}
}
catch (Exception e)
{
HornetQServerLogger.LOGGER.warn(e.getMessage(), e);
return false;
}
}
public void forceDelivery()
{
if (pageSubscription != null && pageSubscription.isPaging())
{
if (isTrace)
{
HornetQServerLogger.LOGGER.trace("Force delivery scheduling depage");
}
scheduleDepage(false);
}
if (isTrace)
{
HornetQServerLogger.LOGGER.trace("Force delivery deliverying async");
}
deliverAsync();
}
public void deliverAsync()
{
if (scheduledRunners.get() < MAX_SCHEDULED_RUNNERS)
{
scheduledRunners.incrementAndGet();
try
{
getExecutor().execute(deliverRunner);
}
catch (RejectedExecutionException ignored)
{
// no-op
scheduledRunners.decrementAndGet();
}
checkDepage();
}
}
public void close() throws Exception
{
if (checkQueueSizeFuture != null)
{
checkQueueSizeFuture.cancel(false);
}
getExecutor().execute(new Runnable()
{
public void run()
{
try
{
cancelRedistributor();
}
catch (Exception e)
{
// nothing that could be done anyway.. just logging
HornetQServerLogger.LOGGER.warn(e.getMessage(), e);
}
}
});
if (addressSettingsRepository != null)
{
addressSettingsRepository.unRegisterListener(addressSettingsRepositoryListener);
}
}
public Executor getExecutor()
{
if (pageSubscription != null && pageSubscription.isPaging())
{
// When in page mode, we don't want to have concurrent IO on the same PageStore
return pageSubscription.getExecutor();
}
else
{
return executor;
}
}
/* Only used on tests */
public void deliverNow()
{
deliverAsync();
flushExecutor();
}
public boolean flushExecutor()
{
boolean ok = internalFlushExecutor(10000);
if (!ok)
{
HornetQServerLogger.LOGGER.errorFlushingExecutorsOnQueue();
}
return ok;
}
private boolean internalFlushExecutor(long timeout)
{
FutureLatch future = new FutureLatch();
getExecutor().execute(future);
boolean result = future.await(timeout);
if (!result)
{
HornetQServerLogger.LOGGER.queueBusy(this.name.toString(), timeout);
}
return result;
}
public synchronized void addConsumer(final Consumer consumer) throws Exception
{
if (HornetQServerLogger.LOGGER.isDebugEnabled())
{
HornetQServerLogger.LOGGER.debug(this + " adding consumer " + consumer);
}
flushDeliveriesInTransit();
consumersChanged = true;
cancelRedistributor();
consumerList.add(new ConsumerHolder(consumer));
consumerSet.add(consumer);
}
public synchronized void removeConsumer(final Consumer consumer)
{
consumersChanged = true;
for (ConsumerHolder holder : consumerList)
{
if (holder.consumer == consumer)
{
if (holder.iter != null)
{
holder.iter.close();
}
consumerList.remove(holder);
break;
}
}
if (pos > 0 && pos >= consumerList.size())
{
pos = consumerList.size() - 1;
}
consumerSet.remove(consumer);
LinkedList<SimpleString> groupsToRemove = null;
for (SimpleString groupID : groups.keySet())
{
if (consumer == groups.get(groupID))
{
if (groupsToRemove == null)
{
groupsToRemove = new LinkedList<SimpleString>();
}
groupsToRemove.add(groupID);
}
}
// We use an auxiliary List here to avoid concurrent modification exceptions on the keySet
// while the iteration is being done.
// Since that's a simple HashMap there's no Iterator's support with a remove operation
if (groupsToRemove != null)
{
for (SimpleString groupID : groupsToRemove)
{
groups.remove(groupID);
}
}
}
public synchronized void addRedistributor(final long delay)
{
if (redistributorFuture != null)
{
redistributorFuture.cancel(false);
futures.remove(redistributorFuture);
}
if (redistributor != null)
{
// Just prompt delivery
deliverAsync();
}
if (delay > 0)
{
if (consumerSet.isEmpty())
{
DelayedAddRedistributor dar = new DelayedAddRedistributor(executor);
redistributorFuture = scheduledExecutor.schedule(dar, delay, TimeUnit.MILLISECONDS);
futures.add(redistributorFuture);
}
}
else
{
internalAddRedistributor(executor);
}
}
public synchronized void cancelRedistributor() throws Exception
{
if (redistributor != null)
{
redistributor.stop();
Redistributor redistributorToRemove = redistributor;
redistributor = null;
removeConsumer(redistributorToRemove);
}
if (redistributorFuture != null)
{
redistributorFuture.cancel(false);
redistributorFuture = null;
}
}
@Override
protected void finalize() throws Throwable
{
if (checkQueueSizeFuture != null)
{
checkQueueSizeFuture.cancel(false);
}
cancelRedistributor();
super.finalize();
}
public synchronized int getConsumerCount()
{
return consumerSet.size();
}
public synchronized Set<Consumer> getConsumers()
{
return new HashSet<Consumer>(consumerSet);
}
public boolean hasMatchingConsumer(final ServerMessage message)
{
for (ConsumerHolder holder : consumerList)
{
Consumer consumer = holder.consumer;
if (consumer instanceof Redistributor)
{
continue;
}
Filter filter1 = consumer.getFilter();
if (filter1 == null)
{
return true;
}
else
{
if (filter1.match(message))
{
return true;
}
}
}
return false;
}
public LinkedListIterator<MessageReference> iterator()
{
return new SynchronizedIterator(messageReferences.iterator());
}
public TotalQueueIterator totalIterator()
{
return new TotalQueueIterator();
}
public synchronized MessageReference removeReferenceWithID(final long id1) throws Exception
{
LinkedListIterator<MessageReference> iterator = iterator();
try
{
MessageReference removed = null;
while (iterator.hasNext())
{
MessageReference ref = iterator.next();
if (ref.getMessage().getMessageID() == id1)
{
iterator.remove();
refRemoved(ref);
removed = ref;
break;
}
}
if (removed == null)
{
// Look in scheduled deliveries
removed = scheduledDeliveryHandler.removeReferenceWithID(id1);
}
return removed;
}
finally
{
iterator.close();
}
}
public synchronized MessageReference getReference(final long id1) throws HornetQException
{
LinkedListIterator<MessageReference> iterator = iterator();
try
{
while (iterator.hasNext())
{
MessageReference ref = iterator.next();
if (ref.getMessage().getMessageID() == id1)
{
return ref;
}
}
return null;
}
finally
{
iterator.close();
}
}
public long getMessageCount()
{
synchronized (this)
{
if (pageSubscription != null)
{
// messageReferences will have depaged messages which we need to discount from the counter as they are
// counted on the pageSubscription as well
return messageReferences.size() + getScheduledCount() +
deliveringCount.get() +
pageSubscription.getMessageCount();
}
else
{
return messageReferences.size() + getScheduledCount() + deliveringCount.get();
}
}
}
public synchronized int getScheduledCount()
{
return scheduledDeliveryHandler.getScheduledCount();
}
public synchronized List<MessageReference> getScheduledMessages()
{
return scheduledDeliveryHandler.getScheduledReferences();
}
public Map<String, List<MessageReference>> getDeliveringMessages()
{
List<ConsumerHolder> consumerListClone = cloneConsumersList();