View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.client;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.api.endpoint.EndpointBuilder;
12  import org.mule.api.endpoint.ImmutableEndpoint;
13  import org.mule.api.transaction.Transaction;
14  import org.mule.api.transaction.TransactionCallback;
15  import org.mule.api.transaction.TransactionConfig;
16  import org.mule.endpoint.EndpointURIEndpointBuilder;
17  import org.mule.endpoint.URIBuilder;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  import org.mule.transaction.MuleTransactionConfig;
21  import org.mule.transaction.TransactionCoordination;
22  import org.mule.transaction.TransactionTemplate;
23  import org.mule.transport.jms.JmsTransactionFactory;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertNull;
32  import static org.junit.Assert.fail;
33  
34  public class MuleClientTransactionTestCase extends FunctionalTestCase
35  {
36  
37      @Override
38      protected String getConfigResources()
39      {
40          return "org/mule/test/integration/client/test-client-jms-mule-config.xml";
41      }
42  
43      @Test
44      public void testTransactionsWithSetRollbackOnly() throws Exception
45      {
46          final MuleClient client = new MuleClient(muleContext);
47          final Map<String, Object> props = new HashMap<String, Object>();
48          props.put("JMSReplyTo", "replyTo.queue");
49          props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
50  
51          // Empty reply queue
52          while (client.request("jms://replyTo.queue", 2000) != null)
53          {
54              // slurp
55          }
56  
57          MuleTransactionConfig tc = new MuleTransactionConfig();
58          tc.setFactory(new JmsTransactionFactory());
59          tc.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
60  
61          // This enpoint needs to be registered prior to use cause we need to set
62          // the transaction config so that the endpoint will "know" it is transacted
63          // and not close the session itself but leave it up to the transaction.
64          EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
65              new URIBuilder("jms://test.queue", muleContext));
66          endpointBuilder.setTransactionConfig(tc);
67          endpointBuilder.setName("TransactedTest.Queue");
68          ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
69                  .getOutboundEndpoint(endpointBuilder);
70          client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
71  
72          TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
73          tt.execute(new TransactionCallback<Void>()
74          {
75              public Void doInTransaction() throws Exception
76              {
77                  for (int i = 0; i < 100; i++)
78                  {
79                      client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
80                  }
81                  Transaction tx = TransactionCoordination.getInstance().getTransaction();
82                  assertNotNull(tx);
83                  tx.setRollbackOnly();
84                  return null;
85              }
86          });
87  
88          MuleMessage result = client.request("jms://replyTo.queue", 2000);
89          assertNull(result);
90      }
91  
92      @Test
93      public void testTransactionsWithExceptionThrown() throws Exception
94      {
95          final MuleClient client = new MuleClient(muleContext);
96          final Map<String, Object> props = new HashMap<String, Object>();
97          props.put("JMSReplyTo", "replyTo.queue");
98          props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
99  
100         // Empty reply queue
101         while (client.request("jms://replyTo.queue", 2000) != null)
102         {
103             // hmm..mesages
104         }
105 
106         MuleTransactionConfig tc = new MuleTransactionConfig();
107         tc.setFactory(new JmsTransactionFactory());
108         tc.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
109 
110         // This enpoint needs to be registered prior to use cause we need to set
111         // the transaction config so that the endpoint will "know" it is transacted
112         // and not close the session itself but leave it up to the transaction.
113         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
114             new URIBuilder("jms://test.queue", muleContext));
115         endpointBuilder.setTransactionConfig(tc);
116         endpointBuilder.setName("TransactedTest.Queue");
117         ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
118                 .getOutboundEndpoint(endpointBuilder);
119         client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
120 
121         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
122         try
123         {
124             tt.execute(new TransactionCallback<Void>()
125             {
126                 public Void doInTransaction() throws Exception
127                 {
128                     for (int i = 0; i < 100; i++)
129                     {
130                         client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
131                     }
132                     throw new Exception();
133                 }
134             });
135             fail();
136         }
137         catch (Exception e)
138         {
139             // this is ok
140         }
141 
142         MuleMessage result = client.request("jms://replyTo.queue", 2000);
143         assertNull(result);
144     }
145 
146     @Test
147     public void testTransactionsWithCommit() throws Exception
148     {
149         final MuleClient client = new MuleClient(muleContext);
150         final Map<String, Object> props = new HashMap<String, Object>();
151         props.put("JMSReplyTo", "replyTo.queue");
152         props.put(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, "false");
153         props.put("transacted", "true");
154 
155         // Empty reply queue
156         while (client.request("jms://replyTo.queue", 2000) != null)
157         {
158             // yum!
159         }
160 
161         MuleTransactionConfig tc = new MuleTransactionConfig();
162         tc.setFactory(new JmsTransactionFactory());
163         tc.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
164 
165         // This enpoint needs to be registered prior to use cause we need to set
166         // the transaction config so that the endpoint will "know" it is transacted
167         // and not close the session itself but leave it up to the transaction.
168         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(
169             new URIBuilder("jms://test.queue", muleContext));
170         endpointBuilder.setTransactionConfig(tc);
171         endpointBuilder.setName("TransactedTest.Queue");
172         ImmutableEndpoint inboundEndpoint = muleContext.getEndpointFactory()
173                 .getOutboundEndpoint(endpointBuilder);
174         client.getMuleContext().getRegistry().registerEndpoint(inboundEndpoint);
175 
176         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
177         tt.execute(new TransactionCallback<Void>()
178         {
179             public Void doInTransaction() throws Exception
180             {
181                 for (int i = 0; i < 100; i++)
182                 {
183                     client.send("jms://test.queue", "Test Client Dispatch message " + i, props);
184                 }
185                 return null;
186             }
187         });
188 
189         for (int i = 0; i < 100; i++)
190         {
191             MuleMessage result = client.request("jms://replyTo.queue", 2000);
192             assertNotNull(result);
193         }
194         MuleMessage result = client.request("jms://replyTo.queue", 2000);
195         assertNull(result);
196     }
197 
198     protected void emptyReplyQueue() throws Exception
199     {
200         final MuleClient client = new MuleClient(muleContext);
201         MuleTransactionConfig tc = new MuleTransactionConfig();
202         tc.setFactory(new JmsTransactionFactory());
203         tc.setAction(TransactionConfig.ACTION_ALWAYS_BEGIN);
204         TransactionTemplate<Void> tt = new TransactionTemplate<Void>(tc, muleContext);
205         tt.execute(new TransactionCallback<Void>()
206         {
207             public Void doInTransaction() throws Exception
208             {
209                 while (client.request("jms://replyTo.queue", 2000) != null)
210                 {
211                     // munch..
212                 }
213 
214                 return null;
215             }
216         });
217     }
218 
219 }