View Javadoc

1   /*
2    * $Id: InboundMessageLossTestCase.java 22431 2011-07-18 07:40:35Z dirk.olmes $
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.reliability;
12  
13  import org.mule.api.context.notification.ExceptionNotificationListener;
14  import org.mule.context.notification.ExceptionNotification;
15  import org.mule.exception.DefaultSystemExceptionStrategy;
16  import org.mule.routing.filters.WildcardFilter;
17  import org.mule.transport.jms.redelivery.MessageRedeliveredException;
18  import org.mule.util.concurrent.Latch;
19  
20  import java.util.concurrent.TimeUnit;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  /**
28   * Verify that no inbound messages are lost when exceptions occur.
29   * The message must either make it all the way to the SEDA queue (in the case of
30   * an asynchronous inbound endpoint), or be restored/rolled back at the source.
31   *
32   * In the case of JMS, this will cause the failed message to be redelivered if
33   * JMSRedelivery is configured.
34   */
35  public class InboundMessageLossTestCase extends AbstractJmsReliabilityTestCase
36  {
37      protected Latch messageRedelivered;
38      protected final int latchTimeout = 5000;
39  
40      @Override
41      protected String getConfigResources()
42      {
43          return "reliability/activemq-config.xml, reliability/inbound-message-loss.xml";
44      }
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          super.doSetUp();
50  
51          // Set SystemExceptionStrategy to redeliver messages (this can only be configured programatically for now)
52          ((DefaultSystemExceptionStrategy) muleContext.getExceptionListener()).setRollbackTxFilter(new WildcardFilter("*"));
53  
54          // Tell us when a MessageRedeliverdException has been handled
55          messageRedelivered = new Latch();
56          muleContext.registerListener(new ExceptionNotificationListener<ExceptionNotification>()
57          {
58              @Override
59              public void onNotification(ExceptionNotification notification)
60              {
61                  if (notification.getException() instanceof MessageRedeliveredException)
62                  {
63                      messageRedelivered.countDown();
64                  }
65              }
66          });
67      }
68  
69      @Test
70      public void testNoException() throws Exception
71      {
72          putMessageOnQueue("noException");
73  
74          // Delivery was successful
75          assertFalse("Message should not have been redelivered",
76              messageRedelivered.await(latchTimeout, TimeUnit.MILLISECONDS));
77      }
78  
79      @Test
80      public void testTransformerException() throws Exception
81      {
82          putMessageOnQueue("transformerException");
83  
84          // Delivery failed so message should have been redelivered
85          assertTrue("Message was not redelivered",
86              messageRedelivered.await(latchTimeout, TimeUnit.MILLISECONDS));
87      }
88  
89      @Test
90      public void testRouterException() throws Exception
91      {
92          putMessageOnQueue("routerException");
93  
94          // Delivery failed so message should have been redelivered
95          assertTrue("Message was not redelivered",
96              messageRedelivered.await(latchTimeout, TimeUnit.MILLISECONDS));
97      }
98  
99      @Test
100     public void testComponentException() throws Exception
101     {
102         putMessageOnQueue("componentException");
103 
104         // Exception occurs after the SEDA queue for an asynchronous request, so from the client's
105         // perspective, the message has been delivered successfully.
106         assertFalse("Message should not have been redelivered",
107             messageRedelivered.await(latchTimeout, TimeUnit.MILLISECONDS));
108     }
109 }