Trying out XA: untested theory

Ok, let’s give XA a try.

0. What do we need?

The Spring AbstractMessageListenerContainer API doc says:

[W]rap the entire processing with an XA transaction, covering the reception of the message as well as the execution of the message listener. This is only supported by DefaultMessageListenerContainer, through specifying a “transactionManager” (typically a JtaTransactionManager, with a corresponding XA-aware JMS ConnectionFactory passed in as “connectionFactory”).

Keith adds that we’ll also need a Hibernate SessionFactory bean… but we’ll leave that out for this first round.

1. The ConnectionFactory Part

For the ConnectionFactory part, we’ll use ActiveMQ’s ConnectionFactory implementation, and it will be XA-aware because we are configuring it as a JNDI datasource (do I have my terms right?) in JBoss.  So, how do we do that?

1.1. Configuring the Datasource

We dropped a file named activemq-jms-ds.xml which he got from somewhere (maybe from an article on ActiveMQ’s site titled Integrating Apache ActiveMQ with JBoss) into the JBoss deploy folder*.  The file starts out something like this:

<!DOCTYPE connection-factories
PUBLIC “-//JBoss//DTD JBOSS JCA Config 1.5//EN”
http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd”&gt;
<connection-factories>
<tx-connection-factory>
<jndi-name>activemq/QueueConnectionFactory</jndi-name>
<xa-transaction/>
<track-connection-by-tx/>
<rar-name>activemq-ra.rar</rar-name>
<connection-definition>javax.jms.QueueConnectionFactory</connection-definition>
<ServerUrl>tcp://localhost</ServerUrl>
<min-pool-size>1</min-pool-size>
<max-pool-size>200</max-pool-size>
<blocking-timeout-millis>30000</blocking-timeout-millis>
<idle-timeout-minutes>3</idle-timeout-minutes>
</tx-connection-factory>
<!– More stuff here… –>
</connection-factories>

Notice that this configuration file specifies XA transactions and points to an activemq-ra.rar file

*Interestingly to me, not to the config folder.

1.1.1. The thing that matters about this

This is a bunch of configuration, but what matters from the Spring bean file’s point of view is the JNDI name, because that’s how we’ll refer to it from there.

1.2. The RAR file

We had also deployed the activemq-ra.rar file to JBoss.  Inside that .rar file, under a meta-inf folder, is an ra.xml file.  From looking at the xsd file, other Sun documentation, and the <display-name> tag within the file, it seems that this file is a Connector Deployment Descriptor for the ActiveMQ JMS Resource Adapter.  Apparently it’s not only a connector, it’s also a resource adapter.  (I bet that’s why there’s an “ra” in the filename.)

At any rate, under connector -> resourceadapter -> outbound-resourceadapter, one of the <connection-definition> tags under there has these entries:

<connectionfactory-interface>javax.jms.QueueConnectionFactory</connectionfactory-interface>
<connectionfactory-impl-class>org.apache.activemq.ra.ActiveMQConnectionFactory</connectionfactory-impl-class>

So it looks like this is how the app server knows what implementation of the javax.jms.QueueConnectionFactory interface ActiveMQ provides.  (I’ve been doing a lot of guessing around — I was missing some background on JCA.  ActiveMQ’s article Integrating Apache ActiveMQ with JBoss, which I didn’t see until this post was half written, helps fill in some of these gaps.)

2. Tying It All Together

There are some other things we’ll need to tie this all together:

  • A bean definition for the queue connection factory to reference
  • A bean definition for a JtaTransactionManager to reference
  • A DefaultMessageListenerContainer bean in our Spring beans file

For the first one, we refer to the Spring manual appendix A.2.3. The jee schema; the latter two are based on the examples in section 19.4.5. Processing messages within transactions.  The concept of referencing a jndi-lookup bean as if it were just a normal bean is from the Java World article, the example where it defines an appJmsDestination bean pointing to the JNDI name and then sets the JmsTemplate’s defaultDestination property to reference this bean.

So here’s the proposed Spring bean file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:jee=”http://www.springframework.org/schema/jee&#8221;
xsi:schemaLocation=” http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd”&gt;
<jee:jndi-lookup id=”queueConnectionFactory” jndi-name=”activemq/QueueConnectionFactory”/>
<bean id=”transactionManager” class=”org.springframework.transaction.jta.JtaTransactionManager”/>

<bean id=”jmsContainer” class=”org.springframework.jms.listener.DefaultMessageListenerContainer”>
<property name=”connectionFactory” ref=”queueConnectionFactory“/>
<property name=”destination” ref=”destination”/>
<property name=”messageListener” ref=”messageListener”/>
<property name=”transactionManager” ref=”transactionManager“/>
</bean>
</beans>

3. It’s All A Bit Backwards…

Now the crazy thing is, none of this may work — I haven’t actually tried any of this.  There are so many new (to me) concepts flying around that I needed to write down what I was going to try to do just to keep track of it all.  (It’s like trying to keep seven concepts in your head when you can only do five at once — you keep losing focus on one of the important concepts and have to work to bring it back while hopefully not losing focus on another one.)

Next time, we’ll see if it works!

About these ads

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.