tags:

views:

196

answers:

1

I have a set of parameters that I need to use to access a JMS queue.

Can anyone supply me with a basic example of how I can send a block of XML to an awaiting server using these parameters. For this initial version I don't mind hardcoding these parameters.

I am currently trying this:

Context ctx = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
Queue queue = (Queue) ctx.lookup("OCP.GET.PRODUCTS.COMSRV");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = queueSession.createSender(queue);
TextMessage message = queueSession.createTextMessage();
message.setText(xmlString);

But I have no idea how to set the parameters for Host, Port, QueueManager or Channel

The parameters supplied to me are

  • Manager: OCP.QMGR
  • Channel: OCP.SVRCONN
  • Port: 14234
  • Host: host.server.com
  • sentToQueue: OCP.GET.PRODUCTS.COMSRV
  • replyToQueue:COMSRV.GET.PRODUCTS.OCP

I am very new to Java and JMS and am starting to drown with this.

+4  A: 

My understanding is that you are trying to connect to MQSeries (QueueManager and Channel are MQ concepts and are not part of the JMS API AFAIK) so I think that you'll have to use MQ specific client API. I'm really not a MQ expert but it seems that the code below (see Implementing vendor-independent JMS solutions) is close to what you are looking for:

MQQueueConnectionFactory qconFactory = new MQQueueConnectionFactory();
qconFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP); //Used when the MQSeries server is on a different host from the client
qconFactory.setQueueManager(queueManager);
qconFactory.setHostName(hostName);
qconFactory.setPort(port);
qconFactory.setChannel(channel);
connection = qconFactory.createQueueConnection();
session1 = connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);.....

As I said, I'm not a MQ specialist but the MQQueueConnectionFactory seems to be aware of most of the things you are talking about.


Side Note:

When using JNDI, you need to set up JNDI environment properties like the initial context factory and the provider url. Basically, these properties are used to declare what class to use to create the initial context and where to find the JNDI server. Obviously, the values of these properties are dependent on the JNDI service you're connecting to.

You can specify environment properties by using the non empty InitialContext constructor and passing the environment parameter to it. For example to connect to BEA WebLogic JNDI service:

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://myhost:7001");
ctx = new InitialContext(p);

Or you can provide a jndi.properties file and use the non-arg InitialContext constructor. For example, to connect to IBM WebSphere JNDI service, you would put a jndi.properties file with the following content on the classpath:

java.naming.provider.url=iiop://myhost:9001
java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory

This second approach is of course more portable as you don't hard code the values of the parameters in the Java code (this might not be an issue though).

Now, in your case, I can't tell if you need this (and even less what values to use) as you didn't provide any detail on your context (like the application server or the JMS provider or the Messaging solution you are trying to connect to).

Pascal Thivent
Very good answer, you explained both methods well. May want to add that running as a thin client will require the JNDI setup you mentioned, along with supporting jars from the vendor.
Robin