View Javadoc

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