简体   繁体   中英

Post a message to a remote JMS queue using JBoss

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

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.

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();
        }
    }
}

Actually I'm using JBoss 4 and JNDI is not difficult to use.

First of all you have to know where your JNDI is running.

In my JBoss (conf\\jboss-service.xml) I have:

<mbean code="org.jboss.naming.NamingService" name="jboss:service=Naming" xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
    ...
    <attribute name="Port">7099</attribute>
    ...
</mbean>

This is important, this is port you want to connect to.

Now you can easily connect to JNDI using this code:

Hashtable<String, String> contextProperties = new Hashtable<String, String>();
contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
contextProperties.put(Context.PROVIDER_URL, "jnp://localhost:7099");

InitialContext initContext = new InitialContext(contextProperties);

Now when you have context, it's very similar to @Nick Holt's answer, except connection factory creation, you have to use:

QueueConnectionFactory connFactory = (QueueConnectionFactory) initContext.lookup("ConnectionFactory");

Also you do not need to create queue if there is deployed some

Queue queue = (Queue) initContext.lookup("queueName");

All the code above was tested with JBoss 4.2.2 GA and JBossMQ (JBossMQ was, if I'm correct, replaced in 4.2.3 with JBoss messaging).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM