-
Notifications
You must be signed in to change notification settings - Fork 912
/
SlowConsumerTest.java
863 lines (647 loc) · 30.4 KB
/
SlowConsumerTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.activemq.artemis.tests.integration.client;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
import org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.MessageHandler;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.core.management.CoreNotificationType;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.settings.impl.AddressFullMessagePolicy;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.core.settings.impl.SlowConsumerPolicy;
import org.apache.activemq.artemis.core.settings.impl.SlowConsumerThresholdMeasurementUnit;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.tests.util.Wait;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.apache.activemq.artemis.utils.collections.ConcurrentHashSet;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
import static org.apache.activemq.artemis.core.settings.impl.SlowConsumerThresholdMeasurementUnit.MESSAGES_PER_DAY;
import static org.apache.activemq.artemis.core.settings.impl.SlowConsumerThresholdMeasurementUnit.MESSAGES_PER_MINUTE;
import static org.apache.activemq.artemis.core.settings.impl.SlowConsumerThresholdMeasurementUnit.MESSAGES_PER_SECOND;
public class SlowConsumerTest extends ActiveMQTestBase {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private int threshold = 10;
private long checkPeriod = 1;
private boolean isNetty = true;
private ActiveMQServer server;
private final SimpleString QUEUE = new SimpleString("ConsumerTestQueue");
private ServerLocator locator;
@Before
@Override
public void setUp() throws Exception {
super.setUp();
server = createServer(true, isNetty);
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(checkPeriod);
addressSettings.setSlowConsumerThreshold(threshold);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_SECOND);
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
addressSettings.setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
addressSettings.setMaxSizeBytes(10 * 1024);
addressSettings.setPageSizeBytes(1024);
server.start();
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
server.createQueue(new QueueConfiguration(QUEUE).setRoutingType(RoutingType.ANYCAST)).getPageSubscription().getPagingStore().startPaging();
locator = createFactory(isNetty);
}
@Test
public void testSlowConsumerKilled() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
final int numMessages = 25;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
Thread.sleep(3000);
try {
consumer.receiveImmediate();
fail();
} catch (ActiveMQObjectClosedException e) {
assertEquals(e.getType(), ActiveMQExceptionType.OBJECT_CLOSED);
}
}
@Test
public void testSlowConsumerKilledAfterBurst() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
final int numMessages = 3 * threshold;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
for (int i = 0; i < threshold; i++) {
consumer.receiveImmediate().individualAcknowledge();
}
Thread.sleep(3 * checkPeriod * 1000);
try {
consumer.receiveImmediate();
fail();
} catch (ActiveMQObjectClosedException e) {
assertEquals(e.getType(), ActiveMQExceptionType.OBJECT_CLOSED);
}
}
@Test
public void testSlowConsumerSparedAfterBurst() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
final int numMessages = 3 * threshold + 1;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
for (int i = 0; i < 3 * threshold; i++) {
consumer.receiveImmediate().individualAcknowledge();
}
Thread.sleep(3 * checkPeriod * 1000);
assertNotNull(consumer.receiveImmediate());
}
@Test
public void testSlowConsumerNotification() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThreshold(10);
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.NOTIFY);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
final int numMessages = 25;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
SimpleString notifQueue = RandomUtil.randomSimpleString();
session.createQueue(new QueueConfiguration(notifQueue).setAddress(ActiveMQDefaultConfiguration.getDefaultManagementNotificationAddress()).setDurable(false));
ClientConsumer notifConsumer = session.createConsumer(notifQueue.toString(), ManagementHelper.HDR_NOTIFICATION_TYPE + "='" + CoreNotificationType.CONSUMER_SLOW + "'");
final CountDownLatch notifLatch = new CountDownLatch(1);
notifConsumer.setMessageHandler(new MessageHandler() {
@Override
public void onMessage(ClientMessage message) {
assertEquals(CoreNotificationType.CONSUMER_SLOW.toString(), message.getObjectProperty(ManagementHelper.HDR_NOTIFICATION_TYPE).toString());
assertEquals(QUEUE.toString(), message.getObjectProperty(ManagementHelper.HDR_ADDRESS).toString());
assertEquals(Integer.valueOf(1), message.getIntProperty(ManagementHelper.HDR_CONSUMER_COUNT));
if (isNetty) {
assertTrue(message.getSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS).toString().startsWith("/127.0.0.1"));
} else {
assertEquals(SimpleString.toSimpleString("invm:0"), message.getSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS));
}
assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_CONNECTION_NAME));
assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_CONSUMER_NAME));
assertNotNull(message.getSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME));
try {
message.acknowledge();
} catch (ActiveMQException e) {
e.printStackTrace();
}
notifLatch.countDown();
}
});
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
assertTrue(notifLatch.await(15, TimeUnit.SECONDS));
}
@Test
public void testSlowConsumerSpared() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(true, true));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
final int numMessages = 5;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
Thread.sleep(3000);
for (int i = 0; i < numMessages; i++) {
assertNotNull(consumer.receive(500));
}
}
@Test
public void testFastThenSlowConsumerSpared() throws Exception {
locator.setAckBatchSize(0);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(true, true));
final ClientSession producerSession = addClientSession(sf.createSession(true, true));
final ClientProducer producer = addClientProducer(producerSession.createProducer(QUEUE));
final AtomicLong messagesProduced = new AtomicLong(0);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
ClientMessage m = createTextMessage(producerSession, "m", true);
// send messages as fast as possible for 3 seconds
while (System.currentTimeMillis() < (start + 3000)) {
try {
producer.send(m);
messagesProduced.incrementAndGet();
} catch (ActiveMQException e) {
e.printStackTrace();
return;
}
}
start = System.currentTimeMillis();
// send 1 msg/second for 10 seconds
while (System.currentTimeMillis() < (start + 10000)) {
try {
producer.send(m);
messagesProduced.incrementAndGet();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
});
t.start();
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
ClientMessage m = null;
long messagesConsumed = 0;
do {
m = consumer.receive(1500);
if (m != null) {
m.acknowledge();
messagesConsumed++;
}
}
while (m != null);
assertEquals(messagesProduced.longValue(), messagesConsumed);
}
@Test
public void testSlowWildcardConsumer() throws Exception {
SimpleString addressAB = new SimpleString("a.b");
SimpleString addressAC = new SimpleString("a.c");
SimpleString address = new SimpleString("a.*");
SimpleString queueName1 = new SimpleString("Q1");
SimpleString queueName2 = new SimpleString("Q2");
SimpleString queueName = new SimpleString("Q");
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThreshold(10);
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
session.createQueue(new QueueConfiguration(queueName1).setAddress(addressAB).setDurable(false));
session.createQueue(new QueueConfiguration(queueName2).setAddress(addressAC).setDurable(false));
session.createQueue(new QueueConfiguration(queueName).setAddress(address).setDurable(false));
ClientProducer producer = session.createProducer(addressAB);
ClientProducer producer2 = session.createProducer(addressAC);
final int numMessages = 20;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m1" + i));
producer2.send(createTextMessage(session, "m2" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(queueName));
session.start();
Thread.sleep(3000);
try {
consumer.receiveImmediate();
fail();
} catch (ActiveMQObjectClosedException e) {
assertEquals(e.getType(), ActiveMQExceptionType.OBJECT_CLOSED);
}
}
@Test
public void testOneMinuteKilledInVM() throws Exception {
testMinuteKilled(false);
}
@Test
public void testOneMinuteKilled() throws Exception {
testMinuteKilled(true);
}
private void testMinuteKilled(boolean netty) throws Exception {
locator.close();
locator = createFactory(netty);
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_MINUTE);
addressSettings.setSlowConsumerThreshold(60);
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 200;
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
session.commit();
ConcurrentHashSet<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
FixedRateConsumer consumer = new FixedRateConsumer(40, MESSAGES_PER_MINUTE, receivedMessages, sf, QUEUE, 0);
consumer.start();
Queue queue = server.locateQueue(QUEUE);
Wait.assertEquals(1, queue::getConsumerCount);
try {
Wait.assertEquals(0, queue::getConsumerCount);
} finally {
consumer.stopRunning();
}
}
@Test
public void testDaysKilled() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_DAY);
addressSettings.setSlowConsumerThreshold(TimeUnit.DAYS.toSeconds(1)); // one message per second
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 200;
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
session.commit();
ConcurrentHashSet<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
FixedRateConsumer consumer = new FixedRateConsumer(30, MESSAGES_PER_MINUTE, receivedMessages, sf, QUEUE, 0);
consumer.start();
Queue queue = server.locateQueue(QUEUE);
Wait.assertEquals(1, queue::getConsumerCount);
try {
Wait.assertEquals(0, queue::getConsumerCount);
} finally {
consumer.stopRunning();
}
}
@Test
public void testDaysKilledPaging() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_DAY);
addressSettings.setSlowConsumerThreshold(TimeUnit.DAYS.toSeconds(1)); // one message per second
addressSettings.setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
addressSettings.setMaxSizeBytes(10 * 1024);
addressSettings.setPageSizeBytes(1024);
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 200;
Queue queue = server.locateQueue(QUEUE);
queue.getPagingStore().startPaging();
Assert.assertTrue(queue.getPagingStore().isPaging());
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
session.commit();
ConcurrentHashSet<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
FixedRateConsumer consumer = new FixedRateConsumer(30, MESSAGES_PER_MINUTE, receivedMessages, sf, QUEUE, 0);
consumer.start();
Wait.assertEquals(1, queue::getConsumerCount);
try {
Wait.assertEquals(0, queue::getConsumerCount);
} finally {
consumer.stopRunning();
}
}
@Test
public void testDaysSurviving() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_DAY);
addressSettings.setSlowConsumerThreshold(TimeUnit.DAYS.toSeconds(1)); // one message per second
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 10;
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
session.commit();
ConcurrentHashSet<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
FixedRateConsumer consumer = new FixedRateConsumer(70, MESSAGES_PER_MINUTE, receivedMessages, sf, QUEUE, 0);
consumer.start();
Queue queue = server.locateQueue(QUEUE);
Wait.assertEquals(1, queue::getConsumerCount);
try {
Wait.assertEquals(messages, queue::getMessagesAcknowledged);
Wait.assertEquals(messages, () -> receivedMessages.size());
} finally {
consumer.stopRunning();
}
Wait.assertEquals(0, queue::getConsumerCount);
}
@Test
public void testMinuteSurviving() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(2);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_MINUTE);
addressSettings.setSlowConsumerThreshold(60);
//addressSettings.sets
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 10;
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
session.commit();
ConcurrentHashSet<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
FixedRateConsumer consumer = new FixedRateConsumer(80, MESSAGES_PER_MINUTE, receivedMessages, sf, QUEUE, 0);
consumer.start();
Queue queue = server.locateQueue(QUEUE);
try {
Wait.assertEquals(messages, queue::getMessagesAcknowledged);
Assert.assertEquals(1, queue.getConsumerCount());
Wait.assertEquals(messages, () -> receivedMessages.size());
} finally {
consumer.stopRunning();
}
Wait.assertEquals(0, queue::getConsumerCount);
}
@Test
public void testKilledOnNoMessagesSoCanBeRebalanced() throws Exception {
AddressSettings addressSettings = new AddressSettings();
addressSettings.setSlowConsumerCheckPeriod(1);
addressSettings.setSlowConsumerThresholdMeasurementUnit(MESSAGES_PER_SECOND);
addressSettings.setSlowConsumerThreshold(0); // if there are no messages pending, kill me
addressSettings.setSlowConsumerPolicy(SlowConsumerPolicy.KILL);
server.getAddressSettingsRepository().removeMatch(QUEUE.toString());
server.getAddressSettingsRepository().addMatch(QUEUE.toString(), addressSettings);
final Queue queue = server.locateQueue(QUEUE);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
int messages = 1;
for (int i = 0; i < messages; i++) {
producer.send(session.createMessage(true));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
consumer.receive(500).individualAcknowledge();
assertEquals(1, queue.getConsumerCount());
// gets whacked!
Wait.assertEquals(0, queue::getConsumerCount, 2000, 100);
}
/**
* This test creates 3 consumers on one queue. A producer sends
* messages at a rate of 2 messages per second. Each consumer
* consumes messages at rate of 1 message per second. The slow
* consumer threshold is 1 message per second.
* Based on the above settings, slow consumer removal will not
* be performed (2 < 3*1), so no consumer will be removed during the
* test, and all messages will be received.
*/
@Test
public void testMultipleConsumersOneQueue() throws Exception {
locator.setAckBatchSize(0);
Queue queue = server.locateQueue(QUEUE);
ClientSessionFactory sf1 = createSessionFactory(locator);
ClientSessionFactory sf2 = createSessionFactory(locator);
ClientSessionFactory sf3 = createSessionFactory(locator);
ClientSessionFactory sf4 = createSessionFactory(locator);
final int messages = 10 * threshold;
FixedRateProducer producer = new FixedRateProducer(threshold * 2, MESSAGES_PER_SECOND, sf1, QUEUE, messages);
final Set<FixedRateConsumer> consumers = new ConcurrentHashSet<>();
final Set<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
consumers.add(new FixedRateConsumer(threshold, MESSAGES_PER_SECOND, receivedMessages, sf2, QUEUE, 1));
consumers.add(new FixedRateConsumer(threshold, MESSAGES_PER_SECOND, receivedMessages, sf3, QUEUE, 2));
consumers.add(new FixedRateConsumer(threshold, MESSAGES_PER_SECOND, receivedMessages, sf4, QUEUE, 3));
try {
producer.start();
for (FixedRateConsumer consumer : consumers) {
consumer.start();
}
producer.join(10000);
Assert.assertEquals(3, queue.getConsumerCount());
Wait.assertEquals(messages, receivedMessages::size);
} finally {
producer.stopRunning();
Assert.assertFalse(producer.failed);
for (FixedRateConsumer consumer : consumers) {
consumer.stopRunning();
Assert.assertFalse(consumer.failed);
}
logger.debug("***report messages received: {}", receivedMessages.size());
logger.debug("***consumers left: {}", consumers.size());
}
}
private class FixedRateProducer extends FixedRateClient {
int messages;
ClientProducer producer;
FixedRateProducer(int rate, SlowConsumerThresholdMeasurementUnit unit, ClientSessionFactory sf, SimpleString queue, int messages) throws ActiveMQException {
super(sf, queue, rate, unit);
this.messages = messages;
}
@Override
protected void prepareWork() throws ActiveMQException {
super.prepareWork();
this.producer = session.createProducer(queue);
}
@Override
protected void doWork(int count) throws Exception {
if (count < messages) {
ClientMessage m = createTextMessage(session, "msg" + count);
producer.send(m);
logger.debug("producer sent a message {}", count);
} else {
this.working = false;
}
}
@Override
public String toString() {
return "Producer";
}
}
private class FixedRateConsumer extends FixedRateClient {
Set<FixedRateConsumer> consumers;
ClientConsumer consumer;
final Set<ClientMessage> receivedMessages;
int id;
FixedRateConsumer(int rate,
SlowConsumerThresholdMeasurementUnit unit,
Set<ClientMessage> receivedMessages,
ClientSessionFactory sf,
SimpleString queue,
int id) throws ActiveMQException {
super(sf, queue, rate, unit);
this.id = id;
this.receivedMessages = receivedMessages;
}
@Override
protected void prepareWork() throws ActiveMQException {
super.prepareWork();
this.consumer = session.createConsumer(queue);
this.session.start();
}
@Override
protected void doWork(int count) throws Exception {
ClientMessage m = this.consumer.receive(1000);
logger.debug("consumer {} got m: {}", id, m);
if (m != null) {
receivedMessages.add(m);
m.acknowledge();
session.commit();
logger.debug(" consumer {} acked {} now total received: {}", id, m.getClass().getName(), receivedMessages.size());
}
}
@Override
protected void handleError(int count, Exception e) {
failed = true;
System.err.println("Got error receiving message " + count + " remove self " + this.id);
e.printStackTrace();
}
@Override
public String toString() {
return "Consumer " + id;
}
}
private abstract class FixedRateClient extends Thread {
protected ClientSessionFactory sf;
protected SimpleString queue;
protected ClientSession session;
protected final int sleepTime;
protected volatile boolean working;
boolean failed;
FixedRateClient(ClientSessionFactory sf, SimpleString queue, int rate, SlowConsumerThresholdMeasurementUnit unit) throws ActiveMQException {
this.sf = sf;
this.queue = queue;
this.sleepTime = (int) (SlowConsumerThresholdMeasurementUnit.unitOf(unit.getValue()).toMillis(1) / rate);
logger.debug("{} has sleepTime = {} which is {} seconds", this.getClass(), sleepTime, TimeUnit.MILLISECONDS.toSeconds(sleepTime));
}
protected void prepareWork() throws ActiveMQException {
this.session = addClientSession(sf.createSession(true, true));
}
@Override
public void run() {
working = true;
try {
prepareWork();
} catch (ActiveMQException e) {
logger.debug("got error in prepareWork(), aborting...");
e.printStackTrace();
return;
}
int count = 0;
while (working) {
try {
doWork(count);
logger.debug("{} sleeping {}", this.getClass().getName(), sleepTime);
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// expected, nothing to be done
} catch (Exception e) {
failed = true;
handleError(count, e);
working = false;
} finally {
count++;
}
}
}
protected abstract void doWork(int count) throws Exception;
protected void handleError(int count, Exception e) {
}
public void stopRunning() {
working = false;
try {
session.close();
this.interrupt();
join(5000);
if (isAlive()) {
fail("Interrupt is not working on Working Thread");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
handleError(0, e);
}
}
}
}