Java EE Resource Adapter Remote Server Configuration Example

This example demonstrates how to configure the Resource adapter to connect to a remote HornetQ server

This example is composed of a message driven bean and a client

MDBRemoteServerClientExample will send a message to the MDB via a queue and wait for the MDB to send a response via a reply queue

configuring the incoming Adapter

MDB's will consume messages via the incoming resource adapter. This can be found under hornetq-ra.rar/META-INF and is called ra.xml. simply update the connector to use the netty connector instead of an invm connector and configure the transport params, as such

              <resourceadapter-class>org.hornetq.ra.HornetQResourceAdapter</resourceadapter-class>
              <config-property>
                 <description>The transport type</description>
                 <config-property-name>ConnectorClassName</config-property-name>
                 <config-property-type>java.lang.String</config-property-type>
                 <config-property-value>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</config-property-value>
              </config-property>
              <config-property>
                 <description>The transport configuration. These values must be in the form of key=val;key=val;</description>
                 <config-property-name>ConnectionParameters</config-property-name>
                 <config-property-type>java.lang.String</config-property-type>
                 <config-property-value>host=127.0.0.1;port=5446</config-property-value>
              </config-property>
        

configuring the outgoing Adapter

This configures a JCA connection factory the JEE components can use to look up the remote HornetQ Server

The default connection factory is found in jms-ds.xml. In this example we have changed this and renamed it as follows

        <tx-connection-factory>
              <jndi-name>RemoteJmsXA</jndi-name>
              <xa-transaction/>
              <rar-name>hornetq-ra.rar</rar-name>
              <connection-definition>org.hornetq.ra.HornetQRAConnectionFactory</connection-definition>
              <config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
              <config-property name="ConnectorClassName" type="java.lang.String">org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</config-property>
              <config-property name="ConnectionParameters" type="java.lang.String">host=127.0.0.1;port=5446</config-property>
              <max-pool-size>20</max-pool-size>
           </tx-connection-factory>
        

Example step-by-step

To deploy and start the HornetQ server, simply type ./build.sh (or build.bat on windows) from the example directory

After the HornetQ server has started start the Application server, simply type ./build.sh deploy (or build.bat deploy on windows) from the example directory

** make sure that JBOSS_HOME is set to the JBoss installation directory

  1. First we need to get an initial context so we can look-up the EJB on the second server from JNDI. This initial context will get it's properties from the jndi.properties file in the directory config
  2.             initialContext = new InitialContext();
             
  3. Look up the MDB's queue
  4.             Queue queue = (Queue) initialContext.lookup("queue/mdbQueue");
             
  5. look up the connection factory
  6.             ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
             
  7. We then create a connection
  8.             connection = cf.createConnection();
             
  9. we then create a session
  10.             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             
  11. we now create a message producer to send the message
  12.             MessageProducer producer = session.createProducer(queue);
             
  13. create a text message and send it
  14.             producer.send(session.createTextMessage("a message"));
             
  15. The MDB receives the text message
  16.             TextMessage tm = (TextMessage)message;
             
  17. The MDB looks up the reply queue
  18.             Queue destQueue = HornetQJMSClient.createQueue("mdbReplyQueue");
             
  19. The MDB creates a connection
  20.             Connection connection = connectionFactory.createConnection();
             
  21. The MDB creates a session
  22.             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             
  23. The MDB creates a message producer to send the message
  24.             MessageProducer producer = session.createProducer(destQueue);
             
  25. The MDB creates and sends a text message
  26.             producer.send(session.createTextMessage("A reply message"));
             
  27. The MDB closes the connection which returns it to the pool
  28.             connection.close();
             
  29. The client now looks up the reply queue
  30.             Queue replyQueue = (Queue) initialContext.lookup("queue/mdbReplyQueue");
             
  31. and creates a message consumer to receive the message
  32.             MessageConsumer consumer = session.createConsumer(replyQueue);
             
  33. starting the connection starts delivery
  34.             connection.start();
             
  35. The message consumer receives the text message
  36.             TextMessage textMessage = (TextMessage) consumer.receive(5000);
             
  37. and we always clear up out JMS resources
  38.             if (initialContext != null)
                {
                   initialContext.close();
                }
                if (connection != null)
                {
                   connection.close();
                 }