Permalink
Browse files

HORNETQ-413 - Fixing TransactionUnitTestCase and the testsuite

  • Loading branch information...
1 parent 331ea11 commit a0d883bf1f8161ec27d0611da7e78e01ff10dd9e @clebertsuconic clebertsuconic committed Jun 13, 2010
View
10 src/config/jboss-as-4/clustered/ra.xml
@@ -58,13 +58,19 @@ Copyright 2009 Red Hat, Inc.
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>host=localhost;port=5445</config-property-value>
      </config-property>
- <!--
<config-property>
- <description>The method to use for locatingthe transactionmanager</description>
+ <description>The class that will locate the transactionmanager</description>
+ <config-property-name>TransactionManagerLocatorMethod</config-property-name>
+ <config-property-type>java.lang.String</config-property-type>
+ <config-property-value>org.hornetq.integration.jboss.tm.JBoss4TransactionManagerLocator</config-property-value>
+ </config-property>
+ <config-property>
+ <description>The method to use for locate the transactionmanager</description>
<config-property-name>TransactionManagerLocatorMethod</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>getTm</config-property-value>
</config-property>
+ <!--
<config-property>
<description>Use A local Transaction instead of XA?</description>
<config-property-name>UseLocalTx</config-property-name>
View
10 src/config/jboss-as-4/non-clustered/ra.xml
@@ -58,13 +58,19 @@ Copyright 2009 Red Hat, Inc.
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>host=localhost;port=5445</config-property-value>
      </config-property>
- <!--
<config-property>
- <description>The method to use for locatingthe transactionmanager</description>
+ <description>The class that will locate the transactionmanager</description>
+ <config-property-name>TransactionManagerLocatorMethod</config-property-name>
+ <config-property-type>java.lang.String</config-property-type>
+ <config-property-value>org.hornetq.integration.jboss.tm.JBoss4TransactionManagerLocator</config-property-value>
+ </config-property>
+ <config-property>
+ <description>The method to use for locate the transactionmanager</description>
<config-property-name>TransactionManagerLocatorMethod</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>getTm</config-property-value>
</config-property>
+ <!--
<config-property>
<description>Use A local Transaction instead of XA?</description>
<config-property-name>UseLocalTx</config-property-name>
View
32 src/main/org/hornetq/ra/HornetQRAManagedConnection.java
@@ -44,6 +44,8 @@
import javax.resource.spi.ManagedConnectionMetaData;
import javax.resource.spi.SecurityException;
import javax.security.auth.Subject;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
import org.hornetq.core.logging.Logger;
@@ -98,6 +100,8 @@
private XASession xaSession;
private XAResource xaResource;
+
+ private final TransactionManager tm;
private boolean inManagedTx;
@@ -110,6 +114,7 @@
*/
public HornetQRAManagedConnection(final HornetQRAManagedConnectionFactory mcf,
final HornetQRAConnectionRequestInfo cri,
+ final TransactionManager tm,
final String userName,
final String password) throws ResourceException
{
@@ -120,6 +125,7 @@ public HornetQRAManagedConnection(final HornetQRAManagedConnectionFactory mcf,
this.mcf = mcf;
this.cri = cri;
+ this.tm = tm;
this.userName = userName;
this.password = password;
eventListeners = Collections.synchronizedList(new ArrayList<ConnectionEventListener>());
@@ -560,7 +566,7 @@ public void onException(final JMSException exception)
*/
protected Session getSession() throws JMSException
{
- if (xaResource != null && inManagedTx)
+ if (xaResource != null && isManagedTx())
{
if (HornetQRAManagedConnection.trace)
{
@@ -571,7 +577,7 @@ protected Session getSession() throws JMSException
}
else
{
- if (inManagedTx)
+ if (isManagedTx())
{
if (HornetQRAManagedConnection.trace)
{
@@ -801,6 +807,28 @@ else if (cri.getType() == HornetQRAConnectionFactory.QUEUE_CONNECTION)
throw new ResourceException(je.getMessage(), je);
}
}
+
+ private boolean isManagedTx()
+ {
+ return inManagedTx || isXA();
+ }
+
+ /**
+ * @return
+ * @throws SystemException
+ */
+ private boolean isXA()
+ {
+ try
+ {
+ return (tm != null && tm.getTransaction() != null);
+ }
+ catch (Exception e)
+ {
+ log.warn(e.getMessage(), e);
+ return false;
+ }
+ }
protected void setInManagedTx(boolean inManagedTx)
{
View
1 src/main/org/hornetq/ra/HornetQRAManagedConnectionFactory.java
@@ -160,6 +160,7 @@ public ManagedConnection createManagedConnection(final Subject subject, final Co
HornetQRAManagedConnection mc = new HornetQRAManagedConnection(this,
cri,
+ ra.getTM(),
credential.getUserName(),
credential.getPassword());
View
29 src/main/org/hornetq/ra/HornetQRAProperties.java
@@ -43,6 +43,14 @@
/** Use Local TX instead of XA */
private Boolean localTx = false;
+
+
+ /** Class used to locate the Transaction Manager.
+ * Using JBoss5 as the default locator */
+ private String transactionManagerLocatorClass = "org.hornetq.integration.jboss.tm.JBoss5TransactionManagerLocator;org.hornetq.integration.jboss.tm.JBoss4TransactionManagerLocator";
+
+ /** Method used to locate the TM */
+ private String transactionManagerLocatorMethod = "getTm;getTM";
/**
* Constructor
@@ -140,6 +148,27 @@ public void setUseLocalTx(final Boolean localTx)
}
+ public void setTransactionManagerLocatorClass(final String transactionManagerLocatorClass)
+ {
+ this.transactionManagerLocatorClass = transactionManagerLocatorClass;
+ }
+
+ public String getTransactionManagerLocatorClass()
+ {
+ return transactionManagerLocatorClass;
+ }
+
+ public String getTransactionManagerLocatorMethod()
+ {
+ return transactionManagerLocatorMethod;
+ }
+
+ public void setTransactionManagerLocatorMethod(final String transactionManagerLocatorMethod)
+ {
+ this.transactionManagerLocatorMethod = transactionManagerLocatorMethod;
+ }
+
+
@Override
public String toString()
View
1 src/main/org/hornetq/ra/HornetQRAXAResource.java
@@ -101,6 +101,7 @@ public void end(final Xid xid, final int flags) throws XAException
}
finally
{
+ managedConnection.setInManagedTx(false);
managedConnection.unlock();
}
}
View
38 src/main/org/hornetq/ra/HornetQResourceAdapter.java
@@ -26,6 +26,7 @@
import javax.resource.spi.ResourceAdapterInternalException;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.resource.spi.work.WorkManager;
+import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
import org.hornetq.api.core.HornetQException;
@@ -95,6 +96,8 @@
private final Map<ActivationSpec, HornetQActivation> activations;
private HornetQConnectionFactory defaultHornetQConnectionFactory;
+
+ private TransactionManager tm;
/**
* Constructor
@@ -112,6 +115,10 @@ public HornetQResourceAdapter()
activations = new ConcurrentHashMap<ActivationSpec, HornetQActivation>();
}
+ public TransactionManager getTM()
+ {
+ return tm;
+ }
/**
* Endpoint activation
*
@@ -192,6 +199,8 @@ public void start(final BootstrapContext ctx) throws ResourceAdapterInternalExce
{
HornetQResourceAdapter.log.trace("start(" + ctx + ")");
}
+
+ locateTM();
this.ctx = ctx;
@@ -1330,10 +1339,13 @@ protected HornetQRAProperties getProperties()
*/
protected void setup() throws HornetQException
{
+
+
defaultHornetQConnectionFactory = createHornetQConnectionFactory(raProperties);
sessionFactory = defaultHornetQConnectionFactory.getCoreFactory();
}
+
public HornetQConnectionFactory getDefaultHornetQConnectionFactory() throws ResourceException
{
if (!configured.getAndSet(true))
@@ -1405,6 +1417,32 @@ else if (discoveryAddress != null)
}
return map;
}
+
+ private void locateTM()
+ {
+ String locatorClasses[] = raProperties.getTransactionManagerLocatorClass().split(";");
+ String locatorMethods[] = raProperties.getTransactionManagerLocatorMethod().split(";");
+
+ for (int i = 0 ; i < locatorClasses.length; i++)
+ {
+ tm = Util.locateTM(locatorClasses[i], locatorMethods[i]);
+ if (tm != null)
+ {
+ break;
+ }
+ }
+
+ if (tm == null)
+ {
+ log.warn("It wasn't possible to lookup for a Transaction Manager through the configured properties TransactionManagerLocatorClass and TransactionManagerLocatorMethod");
+ log.warn("HornetQ Resource Adapter won't be able to set and verify transaction timeouts in certain cases.");
+ }
+ else
+ {
+ log.debug("TM located = " + tm);
+ }
+ }
+
private void setParams(final HornetQConnectionFactory cf,
final ConnectionFactoryProperties overrideProperties)
View
29 src/main/org/hornetq/ra/Util.java
@@ -12,10 +12,14 @@
*/
package org.hornetq.ra;
+import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.naming.Context;
+import javax.transaction.TransactionManager;
+
+import org.hornetq.core.logging.Logger;
/**
* Various utility functions
@@ -26,6 +30,9 @@
*/
public class Util
{
+
+ private static final Logger log = Logger.getLogger(Util.class);
+
/**
* Private constructor
@@ -187,4 +194,26 @@ public static Object lookup(final Context context, final String name, final Clas
return result;
}
+
+
+ /** The Resource adapter can't depend on any provider's specific library. Because of that we use reflection to locate the
+ * transaction manager during startup. */
+ public static TransactionManager locateTM(final String locatorClass, final String locatorMethod)
+ {
+ try
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Class<?> aClass = loader.loadClass(locatorClass);
+ Object o = aClass.newInstance();
+ Method m = aClass.getMethod(locatorMethod);
+ return (TransactionManager)m.invoke(o);
+ }
+ catch (Throwable e)
+ {
+ log.debug(e.getMessage(), e);
+ return null;
+ }
+ }
+
+
}
View
2 src/main/org/hornetq/ra/inflow/HornetQActivation.java
@@ -291,7 +291,7 @@ protected synchronized void setup() throws Exception
try
{
session = setupSession();
- HornetQMessageHandler handler = new HornetQMessageHandler(this, session, i);
+ HornetQMessageHandler handler = new HornetQMessageHandler(this, ra.getTM(), session, i);
handler.setup();
session.start();
handlers.add(handler);
View
12 src/main/org/hornetq/ra/inflow/HornetQActivationSpec.java
@@ -89,10 +89,6 @@
/* use local tx instead of XA*/
private Boolean localTx;
- private String transactionManagerLocatorClass = "org.hornetq.integration.jboss.tm.JBoss5TransactionManagerLocator";
-
- private String transactionManagerLocatorMethod = "getTm";
-
/**
* Constructor
*/
@@ -537,22 +533,22 @@ public void setUseLocalTx(final Boolean localTx)
public void setTransactionManagerLocatorClass(final String transactionManagerLocatorClass)
{
- this.transactionManagerLocatorClass = transactionManagerLocatorClass;
+ log.info("TransactionManagerLocatorClass property on the Activation Setup is deprecated and it has no use. Use the property on the HornetQ Resource Adapter if you need");
}
public String getTransactionManagerLocatorClass()
{
- return transactionManagerLocatorClass;
+ return null;
}
public String getTransactionManagerLocatorMethod()
{
- return transactionManagerLocatorMethod;
+ return null;
}
public void setTransactionManagerLocatorMethod(final String transactionManagerLocatorMethod)
{
- this.transactionManagerLocatorMethod = transactionManagerLocatorMethod;
+ log.info("setTransactionManagerLocatorMethod property on the Activation Setup is deprecated and it has no use. Use the property on the HornetQ Resource Adapter if you need");
}
/**
View
40 src/main/org/hornetq/ra/inflow/HornetQMessageHandler.java
@@ -12,15 +12,13 @@
*/
package org.hornetq.ra.inflow;
-import java.lang.reflect.Method;
import java.util.UUID;
import javax.jms.InvalidClientIDException;
import javax.jms.MessageListener;
import javax.resource.ResourceException;
import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;
-import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import org.hornetq.api.core.HornetQException;
@@ -72,13 +70,14 @@
private final int sessionNr;
- private TransactionManager tm;
+ private final TransactionManager tm;
- public HornetQMessageHandler(final HornetQActivation activation, final ClientSession session, final int sessionNr)
+ public HornetQMessageHandler(final HornetQActivation activation, final TransactionManager tm, final ClientSession session, final int sessionNr)
{
this.activation = activation;
this.session = session;
this.sessionNr = sessionNr;
+ this.tm = tm;
}
public void setup() throws Exception
@@ -247,9 +246,9 @@ public void onMessage(final ClientMessage message)
try
{
- if(activation.getActivationSpec().getTransactionTimeout() > 0)
+ if(activation.getActivationSpec().getTransactionTimeout() > 0 && tm != null)
{
- getTm().setTransactionTimeout(activation.getActivationSpec().getTransactionTimeout());
+ tm.setTransactionTimeout(activation.getActivationSpec().getTransactionTimeout());
}
endpoint.beforeDelivery(HornetQActivation.ONMESSAGE);
beforeDelivery = true;
@@ -300,33 +299,4 @@ public void onMessage(final ClientMessage message)
}
- private TransactionManager getTm()
- {
- if (tm == null)
- {
- try
- {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- Class aClass = loader.loadClass(activation.getActivationSpec().getTransactionManagerLocatorClass());
- Object o = aClass.newInstance();
- Method m = aClass.getMethod(activation.getActivationSpec().getTransactionManagerLocatorMethod());
- tm = (TransactionManager)m.invoke(o);
- }
- catch (Exception e)
- {
- throw new IllegalStateException("unable to create TransactionManager from " + activation.getActivationSpec().getTransactionManagerLocatorClass() +
- "." +
- activation.getActivationSpec().getTransactionManagerLocatorMethod(),
- e);
- }
-
- if (tm == null)
- {
- throw new IllegalStateException("Cannot locate a transaction manager");
- }
- }
-
- return tm;
- }
-
}

0 comments on commit a0d883b

Please sign in to comment.