Sending JMS Messages over XA with WildFly - JBoss AS

User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

This short tutorial shows how you can send JMS messages as part of an XA Transaction with JBoss AS / WildFly.

Messages running as part of an XA Transaction are coordinated by a Resource Manager along with other resources which are partecipating to the XA Transaction. In order to do that, you need to use a specific XA Connection Factory which is available under the JNDI name java:/JmsXA only.

@Resource(mappedName="java:/JmsXA")
ConnectionFactory connFactory;

This is a special JCA Connection factory which pools the Connection and ensures proper XA behavior.

Besides it, check the transaction attribute of the business method to be transacted to REQUIRED (which is the default) if you use container managed transactions (CMT). Or use the UserTransaction object when using bean managed transactions (BMT).

Here is an example of it:

@Stateless
public class JMSService {
 
 
    @Resource(mappedName = "java:jboss/jms/queue/exampleQueue")
    private Queue queueExample;
 
    @Resource(mappedName = "java:/JmsXA")
    private ConnectionFactory cf;
 
    private Connection connection;
    private MessageProducer publisher;
    private Session session;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void sendMessage(String txt) {
 
        try {         
 
            connection = cf.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
            publisher = session.createProducer(queueExample);
 
            connection.start();
 
            TextMessage message = session.createTextMessage(txt);
            publisher.send(message);
 
 
        }
        catch (Exception exc) {
            exc.printStackTrace();
        }
        finally {         
  
          if (publisher != null) try { publisher.close(); } catch (Exception ignore) { }
          if (session != null) try { session.close(); } catch (Exception ignore) { }
          if (connection != null) try { connection.close(); } catch (Exception ignore) { }
   
 
         }
    } 
}

Related articles available on mastertheboss.com

How to configure a Queue in JBoss ?

This article has been moved here: JBoss JMS configuration

How to create a Queue with Jmx Console ?

  Bring up the JMX Console in your browser and look for the sect

JBoss JMS Queue example

The following article shows how to create a simple JMS Queue Prod

JBoss JMS Topic example

The following article shows how to create a simple JMS Topic Publ

JBoss HornetQ simple tutorial

HornetQ is an open source project to build a multi-protocol, embe

How do I configure a Queue/Topic to work in a cluster?

  JBoss AS 5 Just set the Clustered attribute to "true" in your

Follow us on Twitter