Java EE JCA 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

WildFly configuration

The example leverages the Arquillian framework to run a WildFly instance and deploy the MDB.

Configuring the incoming JCA resource adapter

The MDB will consume messages via the inflow JCA resource adapter. This can be configured in 2 ways:

  1. via a pooled-connection-factory in the "messaging" subsystem
  2. via the activation configuration properties on the MDB set either via annotations or ejb-jar.xml

In this example the MDB is annotated with @ResourceAdapter("hornetq-ra-remote.rar") which refers to this pooled-connection-factory:

                    <pooled-connection-factory name="hornetq-ra-remote">
                        <transaction mode="xa"/>
                        <user>guest</user>
                        <password>password</password>
                        <connectors>
                            <connector-ref connector-name="remote-http-connector"/>
                        </connectors>
                        <entries>
                            <entry name="java:/RemoteJmsXA"/>
                        </entries>
                    </pooled-connection-factory>
        

This configuration ensures the MDB will consume from the remote queue.

Configuring the outgoing JCA resource adapter

The same pooled-connection-factory used for JCA inflow also configures an outgoing JCA connection factory which Java EE components can use to send messages to the same remote HornetQ Server.

                    <pooled-connection-factory name="hornetq-ra-remote">
                        <transaction mode="xa"/>
                        <user>guest</user>
                        <password>password</password>
                        <connectors>
                            <connector-ref connector-name="remote-http-connector"/>
                        </connectors>
                        <entries>
                            <entry name="java:/RemoteJmsXA"/>
                        </entries>
                    </pooled-connection-factory>
     

Example step-by-step

download WildFly 8.0.0.Final from here and install.

set the JBOSS_HOME property to point to the WildFly install directory

type mvn verify from the example directory to run

  1. First we need to get an initial context so we can send a JMS message to the second server.
  2.             final Properties env = new Properties();
    
                env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    
                env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    
                env.put(Context.PROVIDER_URL, "http-remoting://localhost:8180");
    
                initialContext = new InitialContext(env);
             
  3. Look up the MDB's queue
  4.             Queue queue = (Queue) initialContext.lookup("queues/mdbQueue");
             
  5. look up the connection factory
  6.             ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
             
  7. We then create a connection
  8.             connection = cf.createConnection("guest", "password");
             
  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("queues/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();
                 }