View Javadoc

1   /*
2    * $Id: JmsConcurrentConsumerExecutionTestCase.java 22805 2011-08-30 11:39:24Z 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  package org.mule.test.integration.transaction;
11  
12  import org.hamcrest.core.IsNull;
13  import org.junit.Test;
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.lifecycle.Callable;
17  import org.mule.construct.Flow;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  import org.mule.util.concurrent.Latch;
21  
22  import java.util.concurrent.CountDownLatch;
23  import java.util.concurrent.TimeUnit;
24  
25  import static org.junit.Assert.assertThat;
26  import static org.junit.Assert.fail;
27  
28  /*
29  EE-2430 - Check that JMS SubReceiver executes concurrently.
30  This test that each SubReceiver is using a different Session since
31  if they are using the same Session activeMQ won't execute them concurrently
32  (since it will not do two onMessage invocations concurrently using the same session)
33  One of the latch.await(..) will fail in that case.
34   */
35  public class JmsConcurrentConsumerExecutionTestCase extends FunctionalTestCase
36  {
37  
38      public static final String MESSAGE = "some message";
39      public static final int TIMEOUT = 3000;
40      public static final int SHORT_TIMEOUT = 500;
41      private static final Latch messageSuccessfulReceived = new Latch();
42      private static final Latch messageFailureReceived = new Latch();
43  
44      @Override
45      protected String getConfigResources()
46      {
47          return "org/mule/test/integration/transaction/jms-concurrent-in-transaction.xml";
48      }
49  
50      @Test
51      public void testTwoMessagesOneRollbackOneCommit() throws Exception
52      {
53          MuleClient muleClient = new MuleClient(muleContext);
54          muleClient.dispatch("jms://in", "failure", null);
55          muleClient.dispatch("jms://in", "success", null);
56          if (!messageSuccessfulReceived.await(TIMEOUT, TimeUnit.MILLISECONDS))
57          {
58              fail("JMS messages didn't execute concurrently, might be using only one Session for more than one transaction");
59          }
60          if (!messageFailureReceived.await(TIMEOUT, TimeUnit.MILLISECONDS))
61          {
62              fail("JMS messages didn't execute concurrently, might be using only one Session for more than one transaction");
63          }
64          Flow flowWithTxConfigured = (Flow) getFlowConstruct("flowWithTxConfigured");
65          flowWithTxConfigured.stop();
66          MuleMessage muleMessage = muleClient.request("jms://in", TIMEOUT);
67          assertThat(muleMessage, IsNull.<Object>notNullValue());
68          muleMessage = muleClient.request("jms://in", SHORT_TIMEOUT);
69          assertThat(muleMessage, IsNull.<Object>nullValue());
70      }
71  
72      public static class SuccessComponent implements Callable
73      {
74          @Override
75          public Object onCall(MuleEventContext eventContext) throws Exception
76          {
77              messageSuccessfulReceived.release();
78              messageFailureReceived.await(TIMEOUT, TimeUnit.MILLISECONDS);
79              return eventContext.getMessage();
80          }
81      }
82  
83      public static class FailureComponent implements Callable
84      {
85          @Override
86          public Object onCall(MuleEventContext eventContext) throws Exception
87          {
88              try
89              {
90                  throw new RuntimeException("something bad happend :)");
91              }
92              finally
93              {
94                  messageFailureReceived.release();
95                  messageSuccessfulReceived.await(TIMEOUT,TimeUnit.MILLISECONDS);
96              }
97          }
98      }
99  }