View Javadoc

1   /*
2    * $Id: JmsRedeliveryTestCase.java 20501 2010-12-08 00:52:19Z 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.MuleMessage;
15  import org.mule.api.client.MuleClient;
16  import org.mule.api.context.notification.ExceptionNotificationListener;
17  import org.mule.context.notification.ExceptionNotification;
18  import org.mule.message.ExceptionMessage;
19  import org.mule.tck.FunctionalTestCase;
20  import org.mule.tck.exceptions.FunctionalTestException;
21  import org.mule.tck.functional.CounterCallback;
22  import org.mule.tck.functional.FunctionalTestComponent;
23  import org.mule.transport.jms.redelivery.MessageRedeliveredException;
24  import org.mule.util.concurrent.Latch;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  
28  public class JmsRedeliveryTestCase extends FunctionalTestCase
29  {
30      private final int timeout = getTestTimeoutSecs() * 1000 / 4;
31      private static final String DESTINATION = "jms://in";
32      private static final int MAX_REDELIVERY = 3;
33  
34      protected String getConfigResources()
35      {
36          return "jms-redelivery.xml";
37      }
38  
39      public void testRedelivery() throws Exception
40      {
41          MuleClient client = muleContext.getClient();
42          // required if broker is not restarted with the test - it tries to deliver those messages to the client
43          // purge the queue
44          while (client.request(DESTINATION, 1000) != null)
45          {
46              logger.warn("Destination " + DESTINATION + " isn't empty, draining it");
47          }
48  
49          FunctionalTestComponent ftc = getFunctionalTestComponent("Bouncer");
50  
51          // whether a MessageRedeliverdException has been fired
52          final Latch mrexFired = new Latch();
53          muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
54          {
55              public void onNotification(ExceptionNotification notification)
56              {
57                  if (notification.getException() instanceof MessageRedeliveredException)
58                  {
59                      mrexFired.countDown();
60                      // Test for MULE-4630
61                      assertEquals(DESTINATION, ((MessageRedeliveredException) notification.getException()).getEndpoint().getEndpointURI().toString());
62                      assertEquals(MAX_REDELIVERY, ((MessageRedeliveredException) notification.getException()).getMaxRedelivery());
63                      assertTrue(((MessageRedeliveredException) notification.getException()).getMuleMessage().getPayload() instanceof javax.jms.Message);
64                  }
65              }
66          });
67  
68          // enhance the counter callback to count, then throw an exception
69          final CounterCallback callback = new CounterCallback()
70          {
71              @Override
72              public void eventReceived(MuleEventContext context, Object Component) throws Exception
73              {
74                  final int count = incCallbackCount();
75                  logger.info("Message Delivery Count is: " + count); 
76                  throw new FunctionalTestException();
77              }
78          };
79          ftc.setEventCallback(callback);
80  
81          client.dispatch(DESTINATION, TEST_MESSAGE, null);
82  
83          Thread.sleep(2000);
84          mrexFired.await(timeout, TimeUnit.MILLISECONDS);
85          assertEquals("MessageRedeliveredException never fired.", 0, mrexFired.getCount());
86          assertEquals("Wrong number of delivery attempts", MAX_REDELIVERY + 1, callback.getCallbackCount());
87  
88          MuleMessage dl = client.request("jms://dead.letter", 1000);
89          assertNotNull(dl);
90          assertTrue(dl.getPayload() instanceof ExceptionMessage);
91          ExceptionMessage em = (ExceptionMessage) dl.getPayload();
92          assertNotNull(em.getException());
93          assertTrue(em.getException() instanceof MessageRedeliveredException);
94      }
95  }