Home > My Work > How to send a tx transactional jms message from a local jboss to a remote jboss (version 4.2.3.GA)

How to send a tx transactional jms message from a local jboss to a remote jboss (version 4.2.3.GA)

To send a jms message from a jboss to a remote jboss destination you have to configure a new JMSResourceAdapter.

This code does not work!

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
    properties.put(Context.PROVIDER_URL, "jnp://192.168.1.131.129:1299");
    InitialContext jndiContext = new InitialContext(properties);

    //[2] Look up connection factory and queue.
    ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup("UIL2XAConnectionFactory");
    Queue queue = (Queue)jndiContext.lookup("Queue/DataTransferQueue");

This code correctly sends the message but the tx transactional behaviour is not implemented

You have to:
1. deploy a new JMSResourceAdapter duplicating the file hajndi-jms-ds.xml e.g. remote-jms-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>

<connection-factories>
 
  <!-- ==================================================================== -->
  <!-- JMS Stuff                                                            -->
  <!-- ==================================================================== -->

  <!-- The JMS provider loader -->
  <mbean code="org.jboss.jms.jndi.JMSProviderLoader"
	 name="jboss.mq:service=JMSProviderLoader,name=RemoteJMSProvider">
    <attribute name="ProviderName">RemoteJMSProvider</attribute>
    <attribute name="ProviderAdapterClass">
      org.jboss.jms.jndi.JNDIProviderAdapter
    </attribute>
    <!-- The combined connection factory -->
    <attribute name="FactoryRef">XAConnectionFactory</attribute>
    <!-- The queue connection factory -->
    <attribute name="QueueFactoryRef">XAConnectionFactory</attribute>
    <!-- The topic factory -->
    <attribute name="TopicFactoryRef">XAConnectionFactory</attribute>
    <!-- Access JMS via HAJNDI -->
    <attribute name="Properties">
       java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
       java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
       java.naming.provider.url=10.4.0.7:1100
    </attribute>
  </mbean>


  <!-- JMS XA Resource adapter, use this to get transacted JMS in beans -->
  <tx-connection-factory>
    <jndi-name>RemoteJmsXA</jndi-name>
    <xa-transaction/>
    <rar-name>jms-ra.rar</rar-name>
    <connection-definition>org.jboss.resource.adapter.jms.JmsConnectionFactory</connection-definition>
    <config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
    <config-property name="JmsProviderAdapterJNDI" type="java.lang.String">java:/RemoteJMSProvider</config-property>
    <max-pool-size>20</max-pool-size>
    <!--security-domain-and-application>JmsXARealm</security-domain-and-application-->
  </tx-connection-factory>
</connection-factories>

2. now you can get the new connection factory in the local jndi (really getting the adapter pointing the remote connection factory) and then get the remote destination via a remote jndi context.

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
    properties.put(Context.PROVIDER_URL, "jnp://10.4.0.7:1100");
    InitialContext localJndiContext = new InitialContext();
    InitialContext remoteJndiContext = new InitialContext(properties);

    //[2] Look up connection factory and queue.
    ConnectionFactory connectionFactory = (ConnectionFactory)localJndiContext.lookup("java:/RemoteJmsXA");
    Queue queue = (Queue)remoteJndiContext.lookup("Queue/DataTransferQueue");
    //now you can send a jms message and the real sending will be done at tx commit time.
     Connection  connection = connectionFactory.createConnection();
     Session session = connection.createSession();
     MessageProducer  sender = session.createProducer(queue);

references:
HowDoIConfigureTheJMSResourceAdapterToUseARemoteConnectionFactory
JBossMQHAOverview

Advertisements
Categories: My Work Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: