In this tutorial we will demonstrate how to retrieve Transaction information from your Java EE applications running on JBoss/WildFly and how to combine this information with the Narayana Transaction Analyser application.
By default the javax.transaction API does not expose details about the single Transaction id which are running on the application server. However, if you need to associate correctly the status of single transactions running on the container this information might be necessary. For this purpose we will need to use Arjuna?s distributed transaction service (JTS) API which are part of the application server distribution (JBoss and WildFly), although as you can imagine this is not a Java EE portable approach.
That being said, here is the trick to catch the Transaction id from within your Enterprise classes, say a CMT EJB:
com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate tx = (com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate) com.arjuna.ats.jta.TransactionManager.transactionManager(); com.arjuna.ats.jta.transaction.Transaction arjunaTM = (com.arjuna.ats.jta.transaction.Transaction)tx.getTransaction(); System.out.println("Transaction UID" +arjunaTM.get_uid());
Compiling the code
In order to compile the above code you need to add the following Maven dependencies:
<dependency> <groupId>org.jboss.narayana.jts</groupId> <artifactId>narayana-jts-jacorb</artifactId> <version>5.0.0.CR2</version> </dependency> <dependency> <groupId>org.jboss.narayana.jts</groupId> <artifactId>narayana-jts-integration</artifactId> <version>5.0.0.CR2</version> </dependency>
Non Maven users can add the org\jboss\jts\main\narayana-jts-jacorb-5.0.0.CR2.jar and the org\jboss\jts\integration\main\narayana-jts-integration-5.0.0.CR2.jar to the Build path. In addition you need to enable at runtime the above dependencies by including in your MANIFEST.MF:
Downloading a Transaction Monitor
The com.arjuna.ats.jta.transaction.Transaction contains pretty a load of useful methods such as getStatus which returns the Status of the current running transaction or getRemainingTimeoutMills() which returns the number of ms available before a TimeOut exception is raised.
If you don't want to write by yourself a full Transaction monitoring tool, my suggestion is to take a look at Narayana Transaction Analyser (http://planet.jboss.org/post/narayana_transaction_analyser_1_0_0_alpha1).
The Narayana Transaction Analyser is a tool for helping to diagnose issues with JTA and JTS transactions in WildFly and JBoss EAP 6. Once deployed, the tool provides a list of all transactions ran within the application server. Detailed information is available for each transaction; such as what participants where involved and how they behaved. This makes it easier to find out what went wrong in the case of a transaction rollback. The Transaction Analyser also supports distributed transactions that use JTS.
Once deployed the nta-full.ear, you can invoke the Web application monitoring tool with: http://localhost:8080/nta/