views:

2865

answers:

2

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

+4  A: 

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.

Nick Holt
A: 

Here is the code we used to connect to the SonicMQ broker using the sonic_Client.jar library:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;


public class JmsClient
{
    public static void main(String[] args) throws JMSException
    {
     ConnectionFactory factory = new progress.message.jclient.ConnectionFactory("tcp://<host>:<port>", "<user>", "<password>");
     Connection connection = factory.createConnection();

     try
     {
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      try
      {
       MessageProducer producer = session.createProducer(session.createQueue("<queue>"));
       try
       {
        producer.send(session.createTextMessage("<message body>"));
       }
       finally
       {
        producer.close();
       }
      }
      finally
      {
       session.close();
      }
     }
     finally
     {
      connection.close();
     }
    }
}
Vincent Robert
Hi Vincent - I'd still recommend popping the QueueConnectionFactory, Queue, etc in JNDI if you're server side, just so you remove the compile-time dependency on the SonicMQ JMS driver.
Nick Holt
Thanks Nick, I understand the advantage to use JNDI. But this task is really minor in our application server and we initially did not plan more than half a day to create it. I wrongly assumed JMS would be simpler. As we already spent 2 days on this one and maybe more if we investigate JNDI, I'm happy right now with this working solution. Again, thanks for the advice, we will investigate this after delivering the version we are working on right now.
Vincent Robert