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
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>
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>
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
jndi.properties
file in the directory config
initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("queue/mdbQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
connection = cf.createConnection();
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("queue/mdbReplyQueue");
MessageConsumer consumer = session.createConsumer(replyQueue);
connection.start();
TextMessage textMessage = (TextMessage) consumer.receive(5000);
if (initialContext != null) { initialContext.close(); } if (connection != null) { connection.close(); }