View Javadoc

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