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 ;
@@ -2015,6 +2016,11 @@ private int iterQueue(final int flushLimit,
2015
2016
QueueIterateAction messageAction ) throws Exception {
2016
2017
int count = 0 ;
2017
2018
int txCount = 0 ;
2019
+
2020
+ if (filter1 != null ) {
2021
+ messageAction .addFilter (filter1 );
2022
+ }
2023
+
2018
2024
// This is to avoid scheduling depaging while iterQueue is happening
2019
2025
// this should minimize the use of the paged executor.
2020
2026
depagePending = true ;
@@ -2033,10 +2039,12 @@ private int iterQueue(final int flushLimit,
2033
2039
while (iter .hasNext () && !messageAction .expectedHitsReached (count )) {
2034
2040
MessageReference ref = iter .next ();
2035
2041
2036
- if (filter1 == null || filter1 .match (ref . getMessage () )) {
2042
+ if (messageAction .match (ref )) {
2037
2043
if (messageAction .actMessage (tx , ref )) {
2038
2044
iter .remove ();
2039
- refRemoved (ref );
2045
+ if (!isLastValue ()) {
2046
+ refRemoved (ref );
2047
+ }
2040
2048
}
2041
2049
txCount ++;
2042
2050
count ++;
@@ -2055,7 +2063,7 @@ private int iterQueue(final int flushLimit,
2055
2063
return count ;
2056
2064
}
2057
2065
2058
- List <MessageReference > cancelled = scheduledDeliveryHandler .cancel (ref -> filter1 == null ? true : filter1 . match ( ref . getMessage ()) );
2066
+ List <MessageReference > cancelled = scheduledDeliveryHandler .cancel (messageAction :: match );
2059
2067
for (MessageReference messageReference : cancelled ) {
2060
2068
messageAction .actMessage (tx , messageReference );
2061
2069
count ++;
@@ -2078,12 +2086,12 @@ private int iterQueue(final int flushLimit,
2078
2086
PagedReference reference = pageIterator .next ();
2079
2087
pageIterator .remove ();
2080
2088
2081
- if (filter1 == null || filter1 .match (reference .getMessage ())) {
2082
- count ++;
2083
- txCount ++;
2089
+ if (messageAction .match (reference )) {
2084
2090
if (!messageAction .actMessage (tx , reference )) {
2085
2091
addTail (reference , false );
2086
2092
}
2093
+ txCount ++;
2094
+ count ++;
2087
2095
} else {
2088
2096
addTail (reference , false );
2089
2097
}
@@ -2401,71 +2409,48 @@ public void run() {
2401
2409
}
2402
2410
2403
2411
@ Override
2404
- public synchronized boolean sendMessageToDeadLetterAddress (final long messageID ) throws Exception {
2405
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2406
- while (iter .hasNext ()) {
2407
- MessageReference ref = iter .next ();
2408
- if (ref .getMessage ().getMessageID () == messageID ) {
2409
- incDelivering (ref );
2410
- sendToDeadLetterAddress (null , ref );
2411
- iter .remove ();
2412
- refRemoved (ref );
2413
- return true ;
2414
- }
2415
- }
2416
- if (pageIterator != null && !queueDestroyed ) {
2417
- while (pageIterator .hasNext ()) {
2418
- PagedReference ref = pageIterator .next ();
2419
- if (ref .getMessage ().getMessageID () == messageID ) {
2420
- incDelivering (ref );
2421
- sendToDeadLetterAddress (null , ref );
2422
- pageIterator .remove ();
2423
- refRemoved (ref );
2424
- return true ;
2425
- }
2426
- }
2412
+ public boolean sendMessageToDeadLetterAddress (final long messageID ) throws Exception {
2413
+
2414
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2415
+
2416
+ @ Override
2417
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2418
+ incDelivering (ref );
2419
+ sendToDeadLetterAddress (tx , ref );
2420
+ return true ;
2427
2421
}
2428
- return false ;
2429
- }
2422
+ }) == 1 ;
2430
2423
}
2431
2424
2432
2425
@ Override
2433
- public synchronized int sendMessagesToDeadLetterAddress (Filter filter ) throws Exception {
2434
-
2426
+ public int sendMessagesToDeadLetterAddress (Filter filter ) throws Exception {
2435
2427
return iterQueue (DEFAULT_FLUSH_LIMIT , filter , new QueueIterateAction () {
2436
2428
2437
2429
@ Override
2438
2430
public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2439
-
2440
2431
incDelivering (ref );
2441
- return sendToDeadLetterAddress (tx , ref );
2432
+ sendToDeadLetterAddress (tx , ref );
2433
+ return true ;
2442
2434
}
2443
2435
});
2444
2436
}
2445
2437
2446
2438
@ Override
2447
- public synchronized boolean moveReference (final long messageID ,
2439
+ public boolean moveReference (final long messageID ,
2448
2440
final SimpleString toAddress ,
2449
2441
final Binding binding ,
2450
2442
final boolean rejectDuplicate ) throws Exception {
2451
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2452
- while (iter .hasNext ()) {
2453
- MessageReference ref = iter .next ();
2454
- if (ref .getMessage ().getMessageID () == messageID ) {
2455
- iter .remove ();
2456
- refRemoved (ref );
2457
- incDelivering (ref );
2458
- try {
2459
- move (null , toAddress , binding , ref , rejectDuplicate , AckReason .NORMAL , null , null , true );
2460
- } catch (Exception e ) {
2461
- decDelivering (ref );
2462
- throw e ;
2463
- }
2464
- return true ;
2465
- }
2443
+
2444
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2445
+
2446
+ @ Override
2447
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2448
+ incDelivering (ref );
2449
+ move (tx , toAddress , binding , ref , rejectDuplicate , AckReason .NORMAL , null , null , true );
2450
+ return true ;
2466
2451
}
2467
- return false ;
2468
- }
2452
+
2453
+ }) == 1 ;
2469
2454
}
2470
2455
2471
2456
@ Override
@@ -2511,7 +2496,7 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2511
2496
}
2512
2497
2513
2498
if (!ignored ) {
2514
- move (null , toAddress , binding , ref , rejectDuplicates , AckReason .NORMAL , null , null , true );
2499
+ move (tx , toAddress , binding , ref , rejectDuplicates , AckReason .NORMAL , null , null , true );
2515
2500
}
2516
2501
2517
2502
return true ;
@@ -2529,26 +2514,22 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2529
2514
}
2530
2515
2531
2516
@ Override
2532
- public synchronized boolean copyReference (final long messageID ,
2517
+ public boolean copyReference (final long messageID ,
2533
2518
final SimpleString toQueue ,
2534
2519
final Binding binding ) throws Exception {
2535
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2536
- while (iter .hasNext ()) {
2537
- MessageReference ref = iter .next ();
2538
- if (ref .getMessage ().getMessageID () == messageID ) {
2539
- try {
2540
- copy (null , toQueue , binding , ref );
2541
- } catch (Exception e ) {
2542
- throw e ;
2543
- }
2544
- return true ;
2545
- }
2520
+
2521
+ return iterQueue (DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction (messageID ) {
2522
+
2523
+ @ Override
2524
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2525
+ copy (tx , toQueue , binding , ref );
2526
+ addTail (ref , false );
2527
+ return true ;
2546
2528
}
2547
- return false ;
2548
- }
2529
+ }) == 1 ;
2549
2530
}
2550
2531
2551
- public synchronized int rerouteMessages (final SimpleString queueName , final Filter filter ) throws Exception {
2532
+ public int rerouteMessages (final SimpleString queueName , final Filter filter ) throws Exception {
2552
2533
return iterQueue (DEFAULT_FLUSH_LIMIT , filter , new QueueIterateAction () {
2553
2534
@ Override
2554
2535
public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
@@ -2617,40 +2598,35 @@ public boolean actMessage(Transaction tx, MessageReference ref) throws Exception
2617
2598
}
2618
2599
2619
2600
@ Override
2620
- public synchronized boolean changeReferencePriority (final long messageID , final byte newPriority ) throws Exception {
2621
- try ( LinkedListIterator < MessageReference > iter = iterator () ) {
2601
+ public boolean changeReferencePriority (final long messageID , final byte newPriority ) throws Exception {
2602
+ return iterQueue ( DEFAULT_FLUSH_LIMIT , null , new QueueIterateAction ( messageID ) {
2622
2603
2623
- while ( iter . hasNext ()) {
2624
- MessageReference ref = iter . next ();
2625
- if ( ref .getMessage ().getMessageID () == messageID ) {
2626
- iter . remove ();
2604
+ @ Override
2605
+ public boolean actMessage ( Transaction tx , MessageReference ref ) throws Exception {
2606
+ ref .getMessage ().setPriority ( newPriority );
2607
+ if ( isLastValue ()) {
2627
2608
refRemoved (ref );
2628
- ref .getMessage ().setPriority (newPriority );
2629
- addTail (ref , false );
2630
- return true ;
2631
2609
}
2610
+ addTail (ref , false );
2611
+ return true ;
2632
2612
}
2633
-
2634
- return false ;
2635
- }
2613
+ }) == 1 ;
2636
2614
}
2637
2615
2638
2616
@ Override
2639
- public synchronized int changeReferencesPriority (final Filter filter , final byte newPriority ) throws Exception {
2640
- try (LinkedListIterator <MessageReference > iter = iterator ()) {
2641
- int count = 0 ;
2642
- while (iter .hasNext ()) {
2643
- MessageReference ref = iter .next ();
2644
- if (filter == null || filter .match (ref .getMessage ())) {
2645
- count ++;
2646
- iter .remove ();
2617
+ public int changeReferencesPriority (final Filter filter , final byte newPriority ) throws Exception {
2618
+ return iterQueue (DEFAULT_FLUSH_LIMIT , filter , new QueueIterateAction () {
2619
+
2620
+ @ Override
2621
+ public boolean actMessage (Transaction tx , MessageReference ref ) throws Exception {
2622
+ ref .getMessage ().setPriority (newPriority );
2623
+ if (isLastValue ()) {
2647
2624
refRemoved (ref );
2648
- ref .getMessage ().setPriority (newPriority );
2649
- addTail (ref , false );
2650
2625
}
2626
+ addTail (ref , false );
2627
+ return true ;
2651
2628
}
2652
- return count ;
2653
- }
2629
+ });
2654
2630
}
2655
2631
2656
2632
@ Override
@@ -4186,13 +4162,23 @@ public void run() {
4186
4162
abstract class QueueIterateAction {
4187
4163
4188
4164
protected Integer expectedHits ;
4165
+ protected Long messageID ;
4166
+ protected Filter filter1 = null ;
4167
+ protected Predicate <MessageReference > match ;
4189
4168
4190
4169
QueueIterateAction (Integer expectedHits ) {
4191
4170
this .expectedHits = expectedHits ;
4171
+ this .match = ref -> filter1 == null ? true : filter1 .match (ref .getMessage ());
4172
+ }
4173
+
4174
+ QueueIterateAction (Long messageID ) {
4175
+ this .expectedHits = 1 ;
4176
+ this .match = ref -> ref .getMessage ().getMessageID () == messageID ;
4192
4177
}
4193
4178
4194
4179
QueueIterateAction () {
4195
4180
this .expectedHits = null ;
4181
+ this .match = ref -> filter1 == null ? true : filter1 .match (ref .getMessage ());
4196
4182
}
4197
4183
4198
4184
/**
@@ -4207,6 +4193,15 @@ abstract class QueueIterateAction {
4207
4193
public boolean expectedHitsReached (int currentHits ) {
4208
4194
return expectedHits != null && currentHits >= expectedHits .intValue ();
4209
4195
}
4196
+
4197
+ public void addFilter (Filter filter1 ) {
4198
+ this .filter1 = filter1 ;
4199
+ }
4200
+
4201
+ public boolean match (MessageReference ref ) {
4202
+ return match .test (ref );
4203
+ }
4204
+
4210
4205
}
4211
4206
4212
4207
// For external use we need to use a synchronized version since the list is not thread safe
0 commit comments