View Javadoc

1   /*
2    * $Id: JmsRedeliveryTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.context.notification.ExceptionNotificationListener;
15  import org.mule.context.notification.ExceptionNotification;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.FunctionalTestCase;
18  import org.mule.tck.exceptions.FunctionalTestException;
19  import org.mule.tck.functional.CounterCallback;
20  import org.mule.tck.functional.FunctionalTestComponent;
21  import org.mule.util.concurrent.Latch;
22  
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  
25  public class JmsRedeliveryTestCase extends FunctionalTestCase
26  {
27      private final int timeout = getTestTimeoutSecs() * 1000 / 4;
28      private static final String DESTINATION = "jms://in";
29  
30      protected String getConfigResources()
31      {
32          return "jms-redelivery.xml";
33      }
34  
35      public void testRedelivery() throws Exception
36      {
37          MuleClient client = new MuleClient(muleContext);
38          // required if broker is not restarted with the test - it tries to deliver those messages to the client
39          // purge the queue
40          while (client.request(DESTINATION, 1000) != null)
41          {
42              logger.warn("Destination " + DESTINATION + " isn't empty, draining it");
43          }
44  
45          FunctionalTestComponent ftc = getFunctionalTestComponent("Bouncer");
46  
47          // whether a MessageRedeliverdException has been fired
48          final Latch mrexFired = new Latch();
49          muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
50          {
51              public void onNotification(ExceptionNotification notification)
52              {
53                  if (notification.getException() instanceof MessageRedeliveredException)
54                  {
55                      mrexFired.countDown();
56                  }
57              }
58          });
59  
60          // enhance the counter callback to count, then throw an exception
61          final CounterCallback callback = new CounterCallback()
62          {
63              @Override
64              public void eventReceived(MuleEventContext context, Object Component) throws Exception
65              {
66                  final int count = incCallbackCount();
67                  logger.info("Message Delivery Count is: " + count); 
68                  throw new FunctionalTestException();
69              }
70          };
71          ftc.setEventCallback(callback);
72  
73          client.dispatch(DESTINATION, "test", null);
74  
75          Thread.sleep(2000);
76          mrexFired.await(timeout, TimeUnit.MILLISECONDS);
77          assertEquals("MessageRedeliveredException never fired.", 0, mrexFired.getCount());
78          assertEquals("Wrong number of delivery attempts", 4, callback.getCallbackCount());
79      }
80  
81  }