Post a message to a remote JMS queue using JBoss

connection, java, jms, message, post

Solution

You'll need to use JMS, create a `QueueConnectionFactory` and go from there. Exactly how you create the `QueueConnectionFactory` will be vendor specific (JMS is basically a driver spec for message queues just as JDBC is for databases) but on IBM MQ it something like this:

MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
connectionFactory.setHostName(<hostname>);
connectionFactory.setPort(<port>);
connectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
connectionFactory.setQueueManager(<queue manager>);
connectionFactory.setChannel("SYSTEM.DEF.SVRCONN");

QueueConnection queueConnection = connectionFactory.createQueueConnection();
QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

Queue queue = queueSession.createQueue(<queue name>);

QueueSender queueSender = session.createSender(queue);
QueueReceiver queueReceiver = session.createReceiver(queue); 

EDIT (following question edit)

The best way to access a remote queue, or any queue for that matter, is to add a `Queue` instance to the JNDI registry. For remote queues this is achieved using MBeans that add the `Queue` instance when the server starts.

Take a look at http://www.jboss.org/community/wiki/UsingWebSphereMQSeriesWithJBossASPart4, which while it's an example with IBM MQ, is essentially what you have to do to connect to any remote queue.

If you look at `jbossmq-destinations-service.xml` and `org.jboss.mq.server.jmx` you'll see the MBeans you need to create in relation to a JBoss queue.

Problem

This looks simple but I can't find a simple answer. I want to open a connection to a remote JMS broker (IP and port are known), open a session to the a specific queue (name known) and post a message to this queue. Is there any simple Java API (standard if possible) to do that ? EDIT Ok I understand now that JMS is a driver spec just like JDBC and not a communication protocol as I thought. Given I am running in JBoss, I still don't understand how to create a JBossConnectionFactory. EDIT I actually gave the problem some thoughts (hmmm) and if JMS needs to be treated the same as JDBC, then I need to use a client provided by my MQ implementation. Since we are using SonicMQ for our broker, I decided to embed the sonic_Client.jar library provided with SonicMQ. This is working in a standalone Java application and in our JBoss service. Thanks for the help

Original source