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
The example leverages the Arquillian framework to run a WildFly instance and deploy the MDB.
The MDB will consume messages via the inflow JCA resource adapter. This can be configured in 2 ways:
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.
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>
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
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);
Queue queue = (Queue) initialContext.lookup("queues/mdbQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
connection = cf.createConnection("guest", "password");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
producer.send(session.createTextMessage("a message"));
TextMessage tm = (TextMessage)message;
Queue destQueue = HornetQJMSClient.createQueue("mdbReplyQueue");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destQueue);
producer.send(session.createTextMessage("A reply message"));
connection.close();
Queue replyQueue = (Queue) initialContext.lookup("queues/mdbReplyQueue");
MessageConsumer consumer = session.createConsumer(replyQueue);
connection.start();
TextMessage textMessage = (TextMessage) consumer.receive(5000);
if (initialContext != null) { initialContext.close(); } if (connection != null) { connection.close(); }