summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/MaxDeliveryCountTest.java
blob: fa0fe7e0b5315474d939fcb19966d87290d08a40 (plain)
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
/*
 *
 * 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.qpid.test.unit.client;

import org.apache.log4j.Logger;

import org.apache.qpid.AMQException;
import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.configuration.ClientProperties;
import org.apache.qpid.server.queue.AMQQueueFactory;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Test that the MaxRedelivery feature works as expected, allowing the client to reject
 * messages during rollback/recover whilst specifying they not be requeued if delivery
 * to an application has been attempted a specified number of times.
 *
 * General approach: specify a set of messages which will cause the test client to then
 * deliberately rollback/recover the session after consuming, and monitor that they are
 * re-delivered the specified number of times before the client rejects them without requeue
 * and then verify that they are not subsequently redelivered.
 *
 * Additionally, the queue used in the test is configured for DLQ'ing, and the test verifies
 * that the messages rejected without requeue are then present on the appropriate DLQ.
 */
public class MaxDeliveryCountTest extends QpidBrokerTestCase
{
    private static final Logger _logger = Logger.getLogger(MaxDeliveryCountTest.class);
    private boolean _failed;
    private String _failMsg;
    private static final int MSG_COUNT = 15;
    private static final int MAX_DELIVERY_COUNT = 2;
    private CountDownLatch _awaitCompletion;

    public void setUp() throws Exception
    {
        //enable DLQ/maximumDeliveryCount support for all queues at the vhost level
        setConfigurationProperty("virtualhosts.virtualhost.test.queues.maximumDeliveryCount",
                String.valueOf(MAX_DELIVERY_COUNT));
        setConfigurationProperty("virtualhosts.virtualhost.test.queues.deadLetterQueues",
                                String.valueOf(true));

        //Ensure management is on
        setConfigurationProperty("management.enabled", "true");
        setConfigurationProperty("management.ssl.enabled", "false");

        // Set client-side flag to allow the server to determine if messages
        // dead-lettered or requeued.
        setTestClientSystemProperty(ClientProperties.REJECT_BEHAVIOUR_PROP_NAME, "server");

        super.setUp();

        boolean durableSub = isDurSubTest();

        //declare the test queue
        Connection consumerConnection = getConnection();
        Session consumerSession = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Destination destination = getDestination(consumerSession, durableSub);
        if(durableSub)
        {
            consumerSession.createDurableSubscriber((Topic)destination, getName()).close();
        }
        else
        {
            consumerSession.createConsumer(destination).close();
        }

        consumerConnection.close();

        //Create Producer put some messages on the queue
        Connection producerConnection = getConnection();
        producerConnection.start();
        Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSession.createProducer(getDestination(producerSession, durableSub));

        for (int count = 1; count <= MSG_COUNT; count++)
        {
            Message msg = producerSession.createTextMessage(generateContent(count));
            msg.setIntProperty("count", count);
            producer.send(msg);
        }

        producerConnection.close();

        _failed = false;
        _awaitCompletion = new CountDownLatch(1);
    }

    private Destination getDestination(Session consumerSession, boolean durableSub) throws JMSException
    {
        if(durableSub)
        {
            return consumerSession.createTopic(getTestQueueName());
        }
        else
        {
            return consumerSession.createQueue(getTestQueueName());
        }
    }

    private String generateContent(int count)
    {
        return "Message " + count + " content.";
    }

    /**
     * Test that Max Redelivery is enforced when using onMessage() on a
     * Client-Ack session.
     */
    public void testAsynchronousClientAckSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.CLIENT_ACKNOWLEDGE, redeliverMsgs, false, false);
    }

    /**
     * Test that Max Redelivery is enforced when using onMessage() on a
     * transacted session.
     */
    public void testAsynchronousTransactedSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.SESSION_TRANSACTED, redeliverMsgs, false, false);
    }

    /**
     * Test that Max Redelivery is enforced when using onMessage() on an
     * Auto-Ack session.
     */
    public void testAsynchronousAutoAckSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.AUTO_ACKNOWLEDGE, redeliverMsgs, false, false);
    }

    /**
     * Test that Max Redelivery is enforced when using onMessage() on a
     * Dups-OK session.
     */
    public void testAsynchronousDupsOkSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.DUPS_OK_ACKNOWLEDGE, redeliverMsgs, false, false);
    }

    /**
     * Test that Max Redelivery is enforced when using recieve() on a
     * Client-Ack session.
     */
    public void testSynchronousClientAckSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(3);
        redeliverMsgs.add(14);

        doTest(Session.CLIENT_ACKNOWLEDGE, redeliverMsgs, true, false);
    }

    /**
     * Test that Max Redelivery is enforced when using recieve() on a
     * transacted session.
     */
    public void testSynchronousTransactedSession() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.SESSION_TRANSACTED, redeliverMsgs, true, false);
    }

    public void testDurableSubscription() throws Exception
    {
        final ArrayList<Integer> redeliverMsgs = new ArrayList<Integer>();
        redeliverMsgs.add(1);
        redeliverMsgs.add(2);
        redeliverMsgs.add(5);
        redeliverMsgs.add(14);

        doTest(Session.SESSION_TRANSACTED, redeliverMsgs, false, true);
    }

    public void doTest(final int deliveryMode, final ArrayList<Integer> redeliverMsgs, final boolean synchronous, final boolean durableSub) throws Exception
    {
        final Connection clientConnection = getConnection();

        final boolean transacted = deliveryMode == Session.SESSION_TRANSACTED ? true : false;
        final Session clientSession = clientConnection.createSession(transacted, deliveryMode);

        MessageConsumer consumer;
        Destination dest = getDestination(clientSession, durableSub);
        AMQQueue checkQueue;
        if(durableSub)
        {
            consumer = clientSession.createDurableSubscriber((Topic)dest, getName());
            checkQueue = new AMQQueue("amq.topic", "clientid" + ":" + getName());
        }
        else
        {
            consumer = clientSession.createConsumer(dest);
            checkQueue = (AMQQueue) dest;
        }

        assertEquals("The queue should have " + MSG_COUNT + " msgs at start",
                MSG_COUNT, ((AMQSession<?,?>) clientSession).getQueueDepth(checkQueue));

        clientConnection.start();

        int expectedDeliveries = MSG_COUNT + ((MAX_DELIVERY_COUNT -1) * redeliverMsgs.size());

        if(synchronous)
        {
            doSynchronousTest(clientSession, consumer, clientSession.getAcknowledgeMode(),
                    MAX_DELIVERY_COUNT, expectedDeliveries, redeliverMsgs);
        }
        else
        {
            addMessageListener(clientSession, consumer, clientSession.getAcknowledgeMode(),
                    MAX_DELIVERY_COUNT, expectedDeliveries, redeliverMsgs);

            try
            {
                if (!_awaitCompletion.await(20, TimeUnit.SECONDS))
                {
                    fail("Test did not complete in 20 seconds.");
                }
            }
            catch (InterruptedException e)
            {
                fail("Unable to wait for test completion");
                throw e;
            }

            if(_failed)
            {
                fail(_failMsg);
            }
        }
        consumer.close();

        //check the source queue is now empty
        assertEquals("The queue should have 0 msgs left", 0, ((AMQSession<?,?>) clientSession).getQueueDepth(checkQueue, true));

        //check the DLQ has the required number of rejected-without-requeue messages
        verifyDLQdepth(redeliverMsgs.size(), clientSession, durableSub);

        if(isBrokerStorePersistent())
        {
            //restart the broker to verify persistence of the DLQ and the messages on it
            clientConnection.close();

            restartBroker();

            final Connection clientConnection2 = getConnection();
            final Session clientSession2 = clientConnection2.createSession(transacted, deliveryMode);
            clientConnection2.start();

            //verify the messages on the DLQ
            verifyDLQcontent(clientConnection2, redeliverMsgs, getTestQueueName(), durableSub);
            clientConnection2.close();
        }
        else
        {

            //verify the messages on the DLQ
            verifyDLQcontent(clientConnection, redeliverMsgs, getTestQueueName(), durableSub);
            clientConnection.close();
        }

    }

    private void verifyDLQdepth(int expected, Session clientSession, boolean durableSub) throws AMQException
    {
        AMQDestination checkQueueDLQ;
        if(durableSub)
        {
            checkQueueDLQ = new AMQQueue("amq.topic", "clientid" + ":" + getName() + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX);
        }
        else
        {
            checkQueueDLQ = new AMQQueue("amq.direct", getTestQueueName() + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX);
        }

        assertEquals("The DLQ should have " + expected + " msgs on it", expected,
                        ((AMQSession<?,?>) clientSession).getQueueDepth(checkQueueDLQ, true));
    }

    private void verifyDLQcontent(Connection clientConnection, List<Integer> redeliverMsgs, String destName, boolean durableSub) throws JMSException
    {
        Session clientSession = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer consumer;
        if(durableSub)
        {
            if (isBroker010())
            {
                consumer = clientSession.createConsumer(clientSession.createQueue("clientid:" +getName() + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX));
            }
            else
            {
                consumer = clientSession.createDurableSubscriber(clientSession.createTopic(destName), getName() + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX);
            }
        }
        else
        {
            consumer = clientSession.createConsumer(
                    clientSession.createQueue(destName + AMQQueueFactory.DEFAULT_DLQ_NAME_SUFFIX));
        }

        //keep track of the message we expect to still be on the DLQ
        List<Integer> outstandingMessages = new ArrayList<Integer>(redeliverMsgs);
        int numMsg = outstandingMessages.size();

        for(int i = 0; i < numMsg; i++)
        {
            Message message = consumer.receive(250);

            assertNotNull("failed to consume expected message " + i + " from DLQ", message);
            assertTrue("message " + i + " was the wrong type", message instanceof TextMessage);

            //using Integer here to allow removing the value from the list, using int
            //would instead result in removal of the element at that index
            Integer msgId = message.getIntProperty("count");

            TextMessage txt = (TextMessage) message;
            _logger.info("Received message " + msgId + " at " + i + " from the DLQ: " + txt.getText());

            assertTrue("message " + i + " was not one of those which should have been on the DLQ",
                    redeliverMsgs.contains(msgId));
            assertTrue("message " + i + " was not one of those expected to still be on the DLQ",
                    outstandingMessages.contains(msgId));
            assertEquals("Message " + i + " content was not as expected", generateContent(msgId), txt.getText());

            //remove from the list of outstanding msgs
            outstandingMessages.remove(msgId);
        }

        if(outstandingMessages.size() > 0)
        {
            String failures = "";
            for(Integer msg : outstandingMessages)
            {
                failures = failures.concat(msg + " ");
            }
            fail("some DLQ'd messages were not found on the DLQ: " + failures);
        }
    }

    private void addMessageListener(final Session session, final MessageConsumer consumer, final int deliveryMode, final int maxDeliveryCount,
                                    final int expectedTotalNumberOfDeliveries, final ArrayList<Integer> redeliverMsgs) throws JMSException
    {
        if(deliveryMode == org.apache.qpid.jms.Session.NO_ACKNOWLEDGE
                || deliveryMode == org.apache.qpid.jms.Session.PRE_ACKNOWLEDGE)
        {
            failAsyncTest("Max Delivery feature is not supported with this acknowledgement mode" +
                        "when using asynchronous message delivery.");
        }

        consumer.setMessageListener(new MessageListener()
        {
            private int _deliveryAttempts = 0; //number of times given message(s) have been seen
            private int _numMsgsToBeRedelivered = 0; //number of messages to rollback/recover
            private int _totalNumDeliveries = 0;
            private int _expectedMessage = 1;

            public void onMessage(Message message)
            {
                if(_failed || _awaitCompletion.getCount() == 0L)
                {
                    //don't process anything else
                    return;
                }

                _totalNumDeliveries++;

                if (message == null)
                {
                    failAsyncTest("Should not get null messages");
                    return;
                }

                try
                {
                    int msgId = message.getIntProperty("count");

                    _logger.info("Received message: " + msgId);

                    //check the message is the one we expected
                    if(_expectedMessage != msgId)
                    {
                        failAsyncTest("Expected message " + _expectedMessage + " , got message " + msgId);
                        return;
                    }

                    _expectedMessage++;

                    //keep track of the overall deliveries to ensure we don't see more than expected
                    if(_totalNumDeliveries > expectedTotalNumberOfDeliveries)
                    {
                        failAsyncTest("Expected total of " + expectedTotalNumberOfDeliveries +
                                " message deliveries, reached " + _totalNumDeliveries);
                    }

                    //check if this message is one chosen to be rolled back / recovered
                    if(redeliverMsgs.contains(msgId))
                    {
                        _numMsgsToBeRedelivered++;

                        //check if next message is going to be rolled back / recovered too
                        if(redeliverMsgs.contains(msgId +1))
                        {
                            switch(deliveryMode)
                            {
                                case Session.SESSION_TRANSACTED:
                                    //skip on to next message immediately
                                    return;
                                case Session.CLIENT_ACKNOWLEDGE:
                                    //skip on to next message immediately
                                    return;
                                case Session.DUPS_OK_ACKNOWLEDGE:
                                    //fall through
                                case Session.AUTO_ACKNOWLEDGE:
                                    //must recover session now or onMessage will ack, so
                                    //just fall through the if
                                    break;
                            }
                        }

                        _deliveryAttempts++; //increment count of times the current rolled back/recovered message(s) have been seen

                        _logger.debug("ROLLBACK/RECOVER");
                        switch(deliveryMode)
                        {
                            case Session.SESSION_TRANSACTED:
                                session.rollback();
                                break;
                            case Session.CLIENT_ACKNOWLEDGE:
                                //fall through
                            case Session.DUPS_OK_ACKNOWLEDGE:
                                //fall through
                            case Session.AUTO_ACKNOWLEDGE:
                                session.recover();
                                break;
                        }

                        if( _deliveryAttempts >= maxDeliveryCount)
                        {
                            //the client should have rejected the latest messages upon then
                            //above recover/rollback, adjust counts to compensate
                            _deliveryAttempts = 0;
                        }
                        else
                        {
                            //the message(s) should be redelivered, adjust expected message
                            _expectedMessage -= _numMsgsToBeRedelivered;
                        }
                        _logger.debug("XXX _expectedMessage: " + _expectedMessage + " _deliveryAttempts : " + _deliveryAttempts + " _numMsgsToBeRedelivered=" + _numMsgsToBeRedelivered);
                        //reset count of messages expected to be redelivered
                        _numMsgsToBeRedelivered = 0;
                    }
                    else
                    {
                        //consume the message
                        switch(deliveryMode)
                        {
                            case Session.SESSION_TRANSACTED:
                                session.commit();
                                break;
                            case Session.CLIENT_ACKNOWLEDGE:
                                message.acknowledge();
                                break;
                            case Session.DUPS_OK_ACKNOWLEDGE:
                                //fall-through
                            case Session.AUTO_ACKNOWLEDGE:
                                //do nothing, onMessage will ack on exit.
                                break;
                        }
                    }

                    if (msgId == MSG_COUNT)
                    {
                        //if this is the last message let the test complete.
                        if (expectedTotalNumberOfDeliveries == _totalNumDeliveries)
                        {
                            _awaitCompletion.countDown();
                        }
                        else
                        {
                            failAsyncTest("Last message received, but we have not had the " +
                                        "expected number of total delivieres. Received " + _totalNumDeliveries + " Expecting : " + expectedTotalNumberOfDeliveries);
                        }
                    }
                }
                catch (JMSException e)
                {
                    failAsyncTest(e.getMessage());
                }
            }
        });
    }

    private void failAsyncTest(String msg)
    {
        _logger.error("Failing test because: " + msg);
        _failMsg = msg;
        _failed = true;
        _awaitCompletion.countDown();
    }

    private void doSynchronousTest(final Session session, final MessageConsumer consumer, final int deliveryMode, final int maxDeliveryCount,
            final int expectedTotalNumberOfDeliveries, final ArrayList<Integer> redeliverMsgs) throws JMSException, AMQException, InterruptedException
   {
        if(deliveryMode == Session.AUTO_ACKNOWLEDGE
                || deliveryMode == Session.DUPS_OK_ACKNOWLEDGE
                || deliveryMode == org.apache.qpid.jms.Session.PRE_ACKNOWLEDGE
                || deliveryMode == org.apache.qpid.jms.Session.NO_ACKNOWLEDGE)
        {
            fail("Max Delivery feature is not supported with this acknowledgement mode" +
                 "when using synchronous message delivery.");
        }

        int _deliveryAttempts = 0; //number of times given message(s) have been seen
        int _numMsgsToBeRedelivered = 0; //number of messages to rollback/recover
        int _totalNumDeliveries = 0;
        int _expectedMessage = 1;

        while(!_failed)
        {
            Message message = consumer.receive(1000);

            _totalNumDeliveries++;

            if (message == null)
            {
                fail("Should not get null messages");
                return;
            }

            try
            {
                int msgId = message.getIntProperty("count");

                _logger.info("Received message: " + msgId);

                //check the message is the one we expected
                assertEquals("Unexpected message.", _expectedMessage, msgId);

                _expectedMessage++;

                //keep track of the overall deliveries to ensure we don't see more than expected
                assertTrue("Exceeded expected total number of deliveries.",
                        _totalNumDeliveries <= expectedTotalNumberOfDeliveries );

                //check if this message is one chosen to be rolled back / recovered
                if(redeliverMsgs.contains(msgId))
                {
                    //keep track of the number of messages we will have redelivered
                    //upon rollback/recover
                    _numMsgsToBeRedelivered++;

                    if(redeliverMsgs.contains(msgId +1))
                    {
                        //next message is going to be rolled back / recovered too.
                        //skip ahead to it
                        continue;
                    }

                    _deliveryAttempts++; //increment count of times the current rolled back/recovered message(s) have been seen

                    switch(deliveryMode)
                    {
                        case Session.SESSION_TRANSACTED:
                            session.rollback();
                            break;
                        case Session.CLIENT_ACKNOWLEDGE:
                            session.recover();

                            //sleep then do a synchronous op to give the broker
                            //time to resend all the messages
                            Thread.sleep(500);
                            ((AMQSession) session).sync();
                            break;
                    }

                    if( _deliveryAttempts >= maxDeliveryCount)
                    {
                        //the client should have rejected the latest messages upon then
                        //above recover/rollback, adjust counts to compensate
                        _deliveryAttempts = 0;
                    }
                    else
                    {
                        //the message(s) should be redelivered, adjust expected message
                        _expectedMessage -= _numMsgsToBeRedelivered;
                    }

                    //As we just rolled back / recovered, we must reset the
                    //count of messages expected to be redelivered
                    _numMsgsToBeRedelivered = 0;
                }
                else
                {
                    //consume the message
                    switch(deliveryMode)
                    {
                        case Session.SESSION_TRANSACTED:
                            session.commit();
                            break;
                        case Session.CLIENT_ACKNOWLEDGE:
                            message.acknowledge();
                            break;
                    }
                }

                if (msgId == MSG_COUNT)
                {
                    //if this is the last message let the test complete.
                    assertTrue("Last message received, but we have not had the " +
                            "expected number of total delivieres",
                            expectedTotalNumberOfDeliveries == _totalNumDeliveries);

                    break;
                }
            }
            catch (JMSException e)
            {
                fail(e.getMessage());
            }
        }
   }

    private boolean isDurSubTest()
    {
        return getTestQueueName().contains("DurableSubscription");
    }
}