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.transaction;
8   
9   import org.mule.api.transaction.Transaction;
10  import org.mule.transaction.TransactionCoordination;
11  
12  import java.util.Map;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * Simple service that receives messages from jdbc or jms and just forward the
19   * interesting part.
20   */
21  public class XABridgeComponent
22  {
23      private static Log log = LogFactory.getLog(XABridgeComponent.class);
24  
25      public static boolean mayRollback = false;
26  
27      /**
28       * If <code>mayRollback</code> has been set to true, the service will mark
29       * the current transaction as rollback only on a 30 percent basis.
30       * 
31       * @throws Exception
32       */
33      protected void mayRollback() throws Exception
34      {
35          if (mayRollback)
36          {
37              Transaction tx = TransactionCoordination.getInstance().getTransaction();
38              if (tx != null)
39              {
40                  if (Math.random() < 0.3)
41                  {
42                      log.info("Marking transaction for rollback");
43                      tx.setRollbackOnly();
44                  }
45              }
46          }
47      }
48  
49      /**
50       * Receive the jdbc message and forward the <code>data</code> part. May mark
51       * the current transaction as rollback only.
52       * 
53       * @param msg
54       * @throws Exception
55       */
56      public Object onJdbcMessage(Map msg) throws Exception
57      {
58          mayRollback();
59          return msg.get("data").toString();
60      }
61  
62      /**
63       * Receive the content of the jms message and forward it. May mark the current
64       * transaction as rollback only.
65       * 
66       * @param msg
67       * @throws Exception
68       */
69      public Object onJmsMessage(String msg) throws Exception
70      {
71          mayRollback();
72          return msg;
73      }
74  
75  }