43
43
import java .util .concurrent .atomic .AtomicLongFieldUpdater ;
44
44
import java .util .concurrent .atomic .AtomicReferenceFieldUpdater ;
45
45
import java .util .concurrent .locks .ReentrantLock ;
46
+ import java .util .function .Predicate ;
46
47
47
48
import org .apache .activemq .artemis .api .config .ActiveMQDefaultConfiguration ;
48
49
import org .apache .activemq .artemis .api .core .ActiveMQException ;
@@ -2006,6 +2007,11 @@ private int iterQueue(final int flushLimit,
2006
2007
QueueIterateAction messageAction ) throws Exception {
2007
2008
int count = 0 ;
2008
2009
int txCount = 0 ;
2010
+
2011
+ if (filter1 != null ) {
2012
+ messageAction .addFilter (filter1 );
2013
+ }
2014
+
2009
2015
// This is to avoid scheduling depaging while iterQueue is happening
2010
2016
// this should minimize the use of the paged executor.
2011
2017
depagePending = true ;
@@ -2024,7 +2030,7 @@ private int iterQueue(final int flushLimit,
2024
2030
while (iter .hasNext () && !messageAction .expectedHitsReached (count )) {
2025
2031
MessageReference ref = iter .next ();
2026
2032
2027
- if (filter1 == null || filter1 .match (ref . getMessage () )) {
2033
+ if (messageAction .match (ref )) {
2028
2034
if (messageAction .actMessage (tx , ref )) {
2029
2035
iter .remove ();
2030
2036
refRemoved (ref );
@@ -2046,7 +2052,7 @@ private int iterQueue(final int flushLimit,
2046
2052
return count ;
2047
2053
}
2048
2054
2049
- List <MessageReference > cancelled = scheduledDeliveryHandler .cancel (ref -> filter1 == null ? true : filter1 .match (ref . getMessage () ));
2055
+ List <MessageReference > cancelled = scheduledDeliveryHandler .cancel (ref -> messageAction .match (ref ));
2050
2056
for (MessageReference messageReference : cancelled ) {
2051
2057
messageAction .actMessage (tx , messageReference );
2052
2058
count ++;
@@ -2069,12 +2075,12 @@ private int iterQueue(final int flushLimit,
2069
2075
PagedReference reference = pageIterator .next ();
2070
2076
pageIterator .remove ();
2071
2077
2072
- if (filter1 == null || filter1 .match (reference .getMessage ())) {
2073
- count ++;
2074
- txCount ++;
2078
+ if (messageAction .match (reference )) {
2075
2079
if (!messageAction .actMessage (tx , reference )) {
2076
2080
addTail (reference , false );
2077
2081
}
2082
+ txCount ++;
2083
+ count ++;
2078
2084
} else {
2079
2085
addTail (reference , false );
2080
2086
}
@@ -2393,43 +2399,27 @@ public void run() {
2393
2399
2394
2400
@ Override
2395
2401
public synchronized boolean sendMessageToDeadLetterAddress (final long messageID ) throws Exception {
2396
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2397
- while (iter .hasNext ()) {
2398
- MessageReference ref = iter .next ();
2399
- if (ref .getMessage ().getMessageID () == messageID ) {
2400
- incDelivering (ref );
2401
- sendToDeadLetterAddress (null , ref );
2402
- iter .remove ();
2403
- refRemoved (ref );
2404
- return true ;
2405
- }
2406
- }
2407
- if (pageIterator != null && !queueDestroyed ) {
2408
- while (pageIterator .hasNext ()) {
2409
- PagedReference ref = pageIterator .next ();
2410
- if (ref .getMessage ().getMessageID () == messageID ) {
2411
- incDelivering (ref );
2412
- sendToDeadLetterAddress (null , ref );
2413
- pageIterator .remove ();
2414
- refRemoved (ref );
2415
- return true ;
2416
- }
2417
- }
2402
+
2403
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2404
+
2405
+ @ Override
2406
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2407
+ incDelivering (ref );
2408
+ sendToDeadLetterAddress (tx , ref );
2409
+ return true ;
2418
2410
}
2419
- return false ;
2420
- }
2411
+ }) == 1 ;
2421
2412
}
2422
2413
2423
2414
@ Override
2424
2415
public synchronized int sendMessagesToDeadLetterAddress (Filter filter ) throws Exception {
2425
-
2426
2416
return iterQueue (DEFAULT_FLUSH_LIMIT , filter , new QueueIterateAction () {
2427
2417
2428
2418
@ Override
2429
2419
public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2430
-
2431
2420
incDelivering (ref );
2432
- return sendToDeadLetterAddress (tx , ref );
2421
+ sendToDeadLetterAddress (tx , ref );
2422
+ return true ;
2433
2423
}
2434
2424
});
2435
2425
}
@@ -2439,24 +2429,17 @@ public synchronized boolean moveReference(final long messageID,
2439
2429
final SimpleString toAddress ,
2440
2430
final Binding binding ,
2441
2431
final boolean rejectDuplicate ) throws Exception {
2442
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2443
- while (iter .hasNext ()) {
2444
- MessageReference ref = iter .next ();
2445
- if (ref .getMessage ().getMessageID () == messageID ) {
2446
- iter .remove ();
2447
- refRemoved (ref );
2448
- incDelivering (ref );
2449
- try {
2450
- move (null , toAddress , binding , ref , rejectDuplicate , AckReason .NORMAL , null , null , true );
2451
- } catch (Exception e ) {
2452
- decDelivering (ref );
2453
- throw e ;
2454
- }
2455
- return true ;
2456
- }
2432
+
2433
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2434
+
2435
+ @ Override
2436
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2437
+ incDelivering (ref );
2438
+ move (tx , toAddress , binding , ref , rejectDuplicate , AckReason .NORMAL , null , null , true );
2439
+ return true ;
2457
2440
}
2458
- return false ;
2459
- }
2441
+
2442
+ }) == 1 ;
2460
2443
}
2461
2444
2462
2445
@ Override
@@ -2502,7 +2485,7 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2502
2485
}
2503
2486
2504
2487
if (!ignored ) {
2505
- move (null , toAddress , binding , ref , rejectDuplicates , AckReason .NORMAL , null , null , true );
2488
+ move (tx , toAddress , binding , ref , rejectDuplicates , AckReason .NORMAL , null , null , true );
2506
2489
}
2507
2490
2508
2491
return true ;
@@ -2523,20 +2506,16 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2523
2506
public synchronized boolean copyReference (final long messageID ,
2524
2507
final SimpleString toQueue ,
2525
2508
final Binding binding ) throws Exception {
2526
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2527
- while (iter .hasNext ()) {
2528
- MessageReference ref = iter .next ();
2529
- if (ref .getMessage ().getMessageID () == messageID ) {
2530
- try {
2531
- copy (null , toQueue , binding , ref );
2532
- } catch (Exception e ) {
2533
- throw e ;
2534
- }
2535
- return true ;
2536
- }
2509
+
2510
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2511
+
2512
+ @ Override
2513
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2514
+ copy (null , toQueue , binding , ref );
2515
+ addTail (ref , false );
2516
+ return true ;
2537
2517
}
2538
- return false ;
2539
- }
2518
+ }) == 1 ;
2540
2519
}
2541
2520
2542
2521
public synchronized int rerouteMessages (final SimpleString queueName , final Filter filter ) throws Exception {
@@ -2609,39 +2588,28 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2609
2588
2610
2589
@ Override
2611
2590
public synchronized boolean changeReferencePriority (final long messageID , final byte newPriority ) throws Exception {
2612
- try ( LinkedListIterator < MessageReference > iter = iterator () ) {
2591
+ return iterQueue ( DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction ( messageID ) {
2613
2592
2614
- while (iter .hasNext ()) {
2615
- MessageReference ref = iter .next ();
2616
- if (ref .getMessage ().getMessageID () == messageID ) {
2617
- iter .remove ();
2618
- refRemoved (ref );
2619
- ref .getMessage ().setPriority (newPriority );
2620
- addTail (ref , false );
2621
- return true ;
2622
- }
2593
+ @ Override
2594
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2595
+ ref .getMessage ().setPriority (newPriority );
2596
+ addTail (ref , false );
2597
+ return true ;
2623
2598
}
2624
-
2625
- return false ;
2626
- }
2599
+ }) == 1 ;
2627
2600
}
2628
2601
2629
2602
@ Override
2630
2603
public synchronized int changeReferencesPriority (final Filter filter , final byte newPriority ) throws Exception {
2631
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2632
- int count = 0 ;
2633
- while (iter .hasNext ()) {
2634
- MessageReference ref = iter .next ();
2635
- if (filter == null || filter .match (ref .getMessage ())) {
2636
- count ++;
2637
- iter .remove ();
2638
- refRemoved (ref );
2639
- ref .getMessage ().setPriority (newPriority );
2640
- addTail (ref , false );
2641
- }
2604
+ return iterQueue (DEFAULT_FLUSH_LIMIT , filter , new QueueIterateAction () {
2605
+
2606
+ @ Override
2607
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2608
+ ref .getMessage ().setPriority (newPriority );
2609
+ addTail (ref , false );
2610
+ return true ;
2642
2611
}
2643
- return count ;
2644
- }
2612
+ });
2645
2613
}
2646
2614
2647
2615
@ Override
@@ -4177,13 +4145,23 @@ public void run() {
4177
4145
abstract class QueueIterateAction {
4178
4146
4179
4147
protected Integer expectedHits ;
4148
+ protected Long messageID ;
4149
+ protected Filter filter1 = null ;
4150
+ protected Predicate <MessageReference > match ;
4180
4151
4181
4152
QueueIterateAction (Integer expectedHits ) {
4182
4153
this .expectedHits = expectedHits ;
4154
+ this .match = ref -> filter1 == null ? true : filter1 .match (ref .getMessage ());
4155
+ }
4156
+
4157
+ QueueIterateAction (Long messageID ) {
4158
+ this .expectedHits = 1 ;
4159
+ this .match = ref -> ref .getMessage ().getMessageID () == messageID ;
4183
4160
}
4184
4161
4185
4162
QueueIterateAction () {
4186
4163
this .expectedHits = null ;
4164
+ this .match = ref -> filter1 == null ? true : filter1 .match (ref .getMessage ());
4187
4165
}
4188
4166
4189
4167
/**
@@ -4198,6 +4176,15 @@ abstract class QueueIterateAction {
4198
4176
public boolean expectedHitsReached (int currentHits ) {
4199
4177
return expectedHits != null && currentHits >= expectedHits .intValue ();
4200
4178
}
4179
+
4180
+ public void addFilter (Filter filter1 ) {
4181
+ this .filter1 = filter1 ;
4182
+ }
4183
+
4184
+ public boolean match (MessageReference ref ) {
4185
+ return match .test (ref );
4186
+ }
4187
+
4201
4188
}
4202
4189
4203
4190
// For external use we need to use a synchronized version since the list is not thread safe
0 commit comments