how to send jms message to mq series

Hi all,

I'm new to JMS and I've been asked to send an xml file to a IBM MQ series message queue.

I need to send this message from an application running on a tomcat webserver.

Basically, what packages do I need to start with that?

Is that possible without installing an MQ client on the webserver?

What parameters would I need to make a connection? I currently only know the QUEUE name and that the MQ is running on a different machine than the webserver is.

Thank you!

Steven

[527 byte] By [tombatorea] at [2007-11-27 7:39:25]
# 1

There are a whole load of IBM jars you need installed (sorry I can't remember exactly which ones off the top of my head). You'll also need the jms specification jar which should be distributed with the IBM ones.

There are two options for connecting to MQ

1. Via JNDI

MQ Server doesn't include a JNDI server and I've never tried configuring Tomcat's read only JNDI server for MQ so unless there's a preconfigured application server somewhere (probably WebSphere) already configured with the JNDI objects you need, then get the connection factory and queue via JNDI lookups. If not go with option 2.

2. Using the com.ibm.mq.jms.MQQueueConnectionFactory and com.ibm.mq.jms.MQQueue classes.

Directly instantiating the IBM classes is considered bad practice so I prefer to use Spring. The following example works shows the config for this...

http://www.java-answers.com/index.php?topic=20.0

You'll need to ask for the queue manager name, server hostname/ip, port and channel.

n.b. The example was written for Spring v1 but will work with Spring 2, however there are now better ways of references static fields.

SteveNaivea at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi Steve,I am also in the same position ...Could you please help me setting up the package..Am using weblogic server in the client side and need to send jms message to MQ channel with out installing mq client in the client side..regardsmdave
dave_Watsona at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Hi Tom,Did u get any success on ur try?regards,Dave
dave_Watsona at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
OK, I've asked a couple of mates to send me the list of jars you need.Using the spring framework will simplify the remaining tasks. Is there any problem using it with your application? (it's just another set of jars and some config files)
SteveNaivea at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Mates have come back - you'll probably need to following jars in your class pathjndi.jarproviderutil.jarfscontext.jarcom.ibm.mqjms.jarconnector.jar
SteveNaivea at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Thanks steve..!!!
dave_Watsona at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

This can help u writing to MQueue

==================================================================

//

// Program Name

// MQWrite

//

// Last date of modification

// 1 Oct 2000

//

// Description

// This java class will read a line of input from the keyboard

// and send it as a message. The program will loop until the

// user presses CTL^Z.

//

// Sample Command Line Parameters

// -h 127.0.0.1 -p 1414 -c CLIENT.CHANNEL -m MQA1 -q TEST.QUEUE

//

// Copyright(C), Roger Lacroix, Capitalware

//

//

import com.ibm.mq.*;

import java.io.IOException;

import java.util.Hashtable;

import java.io.*;

public class MQWrite {

private MQQueueManager _queueManager = null;

private Hashtable params = null;

public int port = 1414;

public String hostname= "127.0.0.1";

public String channel= "CLIENT.TO.MQA1";

public String qManager= "MQA1";

public String outputQName = "SYSTEM.DEFAULT.LOCAL.QUEUE";

public MQWrite()

{

super();

}

private boolean allParamsPresent()

{

boolean b = params.containsKey("-h") &&

params.containsKey("-p") &&

params.containsKey("-c") &&

params.containsKey("-m") &&

params.containsKey("-q");

if (b)

{

try

{

port = Integer.parseInt((String) params.get("-p"));

}

catch (NumberFormatException e)

{

b = false;

}

// Set up MQ environment

hostname = (String) params.get("-h");

channel = (String) params.get("-c");

qManager = (String) params.get("-m");

outputQName = (String) params.get("-q");

}

return b;

}

private void init(String[] args) throws IllegalArgumentException

{

params = new Hashtable(5);

if (args.length > 0 && (args.length % 2) == 0)

{

for (int i = 0; i < args.length; i+=2)

{

params.put(args, args[i+1]);

}

}

else

{

throw new IllegalArgumentException();

}

if (allParamsPresent())

{

// Set up MQ environment

MQEnvironment.hostname = hostname;

MQEnvironment.channel = channel;

MQEnvironment.port= port;

}

else

{

throw new IllegalArgumentException();

}

}

public static void main(String[] args)

{

MQWrite write = new MQWrite();

try

{

write.init(args);

write.selectQMgr();

write.write();

}

catch (IllegalArgumentException e)

{

System.out.println("Usage: java MQWrite <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName>");

System.exit(1);

}

catch (MQException e)

{

System.out.println(e);

System.exit(1);

}

}

private void selectQMgr() throws MQException

{

_queueManager = new MQQueueManager(qManager);

}

private void write() throws MQException

{

String line;

int lineNum=0;

int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;

try

{

MQQueue queue = _queueManager.accessQueue( outputQName,

openOptions,

null,// default q manager

null,// no dynamic q name

null ); // no alternate user id

DataInputStream input = new DataInputStream(System.in);

System.out.println("MQWrite v1.0 connected");

System.out.println("and ready for input, terminate with ^Z\n\n");

// Define a simple MQ message, and write some text in UTF format..

MQMessage sendmsg= new MQMessage();

sendmsg.format= MQC.MQFMT_STRING;

sendmsg.feedback= MQC.MQFB_NONE;

sendmsg.messageType = MQC.MQMT_DATAGRAM;

sendmsg.replyToQueueName= "ROGER.QUEUE";

sendmsg.replyToQueueManagerName = qManager;

MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same

// as MQPMO_DEFAULT constant

while ((line = input.readLine()) != null)

{

sendmsg.clearMessage();

sendmsg.messageId= MQC.MQMI_NONE;

sendmsg.correlationId = MQC.MQCI_NONE;

sendmsg.writeString(line);

// put the message on the queue

queue.put(sendmsg, pmo);

System.out.println(++lineNum + ": " + line);

}

queue.close();

_queueManager.disconnect();

}

catch (com.ibm.mq.MQException mqex)

{

System.out.println(mqex);

}

catch (java.io.IOException ioex)

{

System.out.println("An MQ IO error occurred : " + ioex);

}

}

}

//

// Description

// This java class will read a line of input from the keyboard

// and send it as a message. The program will loop until the

// user presses CTL^Z.

//

// Sample Command Line Parameters

// -h 127.0.0.1 -p 1414 -c CLIENT.CHANNEL -m MQA1 -q TEST.QUEUE

//

//

//

import com.ibm.mq.*;

import java.io.IOException;

import java.util.Hashtable;

import java.io.*;

public class MQWrite {

private MQQueueManager _queueManager = null;

private Hashtable params = null;

public int port = 1414;

public String hostname= "127.0.0.1";

public String channel= "CLIENT.TO.MQA1";

public String qManager= "MQA1";

public String outputQName = "SYSTEM.DEFAULT.LOCAL.QUEUE";

public MQWrite()

{

super();

}

private boolean allParamsPresent()

{

boolean b = params.containsKey("-h") &&

params.containsKey("-p") &&

params.containsKey("-c") &&

params.containsKey("-m") &&

params.containsKey("-q");

if (b)

{

try

{

port = Integer.parseInt((String) params.get("-p"));

}

catch (NumberFormatException e)

{

b = false;

}

// Set up MQ environment

hostname = (String) params.get("-h");

channel = (String) params.get("-c");

qManager = (String) params.get("-m");

outputQName = (String) params.get("-q");

}

return b;

}

private void init(String[] args) throws IllegalArgumentException

{

params = new Hashtable(5);

if (args.length > 0 && (args.length % 2) == 0)

{

for (int i = 0; i < args.length; i+=2)

{

params.put(args, args[i+1]);

}

}

else

{

throw new IllegalArgumentException();

}

if (allParamsPresent())

{

// Set up MQ environment

MQEnvironment.hostname = hostname;

MQEnvironment.channel = channel;

MQEnvironment.port= port;

}

else

{

throw new IllegalArgumentException();

}

}

public static void main(String[] args)

{

MQWrite write = new MQWrite();

try

{

write.init(args);

write.selectQMgr();

write.write();

}

catch (IllegalArgumentException e)

{

System.out.println("Usage: java MQWrite <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName>");

System.exit(1);

}

catch (MQException e)

{

System.out.println(e);

System.exit(1);

}

}

private void selectQMgr() throws MQException

{

_queueManager = new MQQueueManager(qManager);

}

private void write() throws MQException

{

String line;

int lineNum=0;

int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;

try

{

MQQueue queue = _queueManager.accessQueue( outputQName,

openOptions,

null,// default q manager

null,// no dynamic q name

null ); // no alternate user id

DataInputStream input = new DataInputStream(System.in);

System.out.println("MQWrite v1.0 connected");

System.out.println("and ready for input, terminate with ^Z\n\n");

// Define a simple MQ message, and write some text in UTF format..

MQMessage sendmsg= new MQMessage();

sendmsg.format= MQC.MQFMT_STRING;

sendmsg.feedback= MQC.MQFB_NONE;

sendmsg.messageType = MQC.MQMT_DATAGRAM;

sendmsg.replyToQueueName= "ROGER.QUEUE";

sendmsg.replyToQueueManagerName = qManager;

MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, same

// as MQPMO_DEFAULT constant

while ((line = input.readLine()) != null)

{

sendmsg.clearMessage();

sendmsg.messageId= MQC.MQMI_NONE;

sendmsg.correlationId = MQC.MQCI_NONE;

sendmsg.writeString(line);

// put the message on the queue

queue.put(sendmsg, pmo);

System.out.println(++lineNum + ": " + line);

}

queue.close();

_queueManager.disconnect();

}

catch (com.ibm.mq.MQException mqex)

{

System.out.println(mqex);

}

catch (java.io.IOException ioex)

{

System.out.println("An MQ IO error occurred : " + ioex);

}

}

}

EAI_Geeka at 2007-7-12 19:20:06 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...