Tuesday, August 11, 2009

Putting the message into IBM Websphere MQ

Like pervious article, we are going to take a look onto the source code to put the message into the MQ.

Entries in the properties file [ProducerProperties.properties]:

host=hostname where MQ is installed; localhost if installed locally
port=port on to which connection is established to the channedl
channel=SYSTEM.DEF.SVRCONN
queueManagerName=Queue Manager Name
destinationName=Queue Name where the message will go
isTopic=false
samplefilelocation=./Company.xml

Source Code

import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import java.io.*;
import java.io.FileInputStream;
import java.util.Properties;
import javax.jms.*;

public class MQProducer {

public MQProducer() {
}

private void putMessageIntoMQ() {
String propertiesFile = "./ProducerProperties.properties";
this.readPropertyFile(propertiesFile);
Connection connection = null;
Session session = null;
javax.jms.Destination destination = null;
MessageProducer producer = null;
try {
JmsFactoryFactory ff = JmsFactoryFactory.getInstance("com.ibm.msg.client.wmq");
JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setStringProperty("XMSC_WMQ_HOST_NAME", host);
cf.setIntProperty("XMSC_WMQ_PORT", port);
cf.setStringProperty("XMSC_WMQ_CHANNEL", channel);
cf.setIntProperty("XMSC_WMQ_CONNECTION_MODE", 1);
cf.setStringProperty("XMSC_WMQ_QUEUE_MANAGER", queueManagerName);
cf.setStringProperty("XMSC_USERID","username"); //username to connect to MQ
cf.setStringProperty("XMSC_PASSWORD","password"); // password to connect to MQ
connection = cf.createConnection();
session = connection.createSession(false, 1);
if(isTopic)
destination = session.createTopic(destinationName);
else
destination = session.createQueue(destinationName);
producer = session.createProducer(destination);
long uniqueNumber = System.currentTimeMillis() % 1000L;
TextMessage message = session.createTextMessage();
message.setText(sampleFile);
connection.start();
producer.send(message);
System.out.println((new StringBuilder()).append("Sent message:\n").append(message).toString());
recordSuccess();
} catch(JMSException jmsex) {
recordFailure(jmsex);
} finally {
if(producer != null)
try {
producer.close();
} catch(JMSException jmsex) {
System.out.println("Producer could not be closed.");
recordFailure(jmsex);
}
if(session != null)
try {
session.close();
} catch(JMSException jmsex) {
System.out.println("Session could not be closed.");
recordFailure(jmsex);
}
if(connection != null)
try {
connection.close();
} catch(JMSException jmsex) {
System.out.println("Connection could not be closed.");
recordFailure(jmsex);
}
}
System.exit(status);
}

private static void processJMSException(JMSException jmsex) {
System.out.println(jmsex);
Throwable innerException = jmsex.getLinkedException();
if(innerException != null)
System.out.println("Inner exception(s):");
for(; innerException != null; innerException = innerException.getCause())
System.out.println(innerException);

}

private static void recordSuccess() {
System.out.println("SUCCESS");
status = 0;
}

private static void recordFailure(Exception ex) {
if(ex != null)
if(ex instanceof JMSException)
processJMSException((JMSException)ex);
else
System.out.println(ex);
System.out.println("FAILURE");
status = -1;
}

private static String host;
private static int port;
private static String channel;
private static String queueManagerName;
private static String destinationName;
private static boolean isTopic;
private static int status = 1;
private static String sampleFile;





private void readPropertyFile(String fileName) { // reading from the property file
try {
Properties mqProperties = new Properties();
FileInputStream fileInputStream = new FileInputStream(fileName);
mqProperties.load(fileInputStream);
host = mqProperties.getProperty("host");
port = Integer.parseInt(mqProperties.getProperty("port"));
channel = mqProperties.getProperty("channel");
queueManagerName = mqProperties.getProperty("queueManagerName");
destinationName = mqProperties.getProperty("destinationName");
isTopic = Boolean.parseBoolean(mqProperties.getProperty("isTopic"));
sampleFile = mqProperties.getProperty("samplefilelocation");
fileInputStream.close();
} catch (Exception exp) {
exp.printStackTrace();
}
}


public static void main(String args[]) {
MQProducer mqProducer = new MQProducer();
mqProducer.putMessageIntoMQ();
}
}






Below is the output in console after run:




10 comments:

Ashif said...
This comment has been removed by the author.
Ashif said...

Hi Snehanshu,

Can you pleas let me know which jar files are used for the below two.

1)import com.ibm.msg.client.jms.JmsConnectionFactory;
2)import com.ibm.msg.client.jms.JmsFactoryFactory;


Kindly also copy me in the below email those jar files please.

ashif.tcs@gmail.com

Anonymous said...

Thanks for the program,
I am trying to modify it to suite my requirement but couldnt do it. Is there something you could think of?
Here is my requirement.

I have a table with a column which holds CLOB data(XMLs) which I exported to a flat file. I want to post the content of the file on to MQ. Say if I exported 10 rows, here xmls in my case to a single flat file which I will be using as a input file to the MQ queue, now the program should recognize 10 messages in file and post each xml as one message rather than posting the entire contents of the file as one message.

Can you make changes to that and update post it in your blog?

Anonymous said...

Hi Snehanshu,

could you please provide jar files for the following ?

1)import com.ibm.msg.client.jms.JmsConnectionFactory;
2)import com.ibm.msg.client.jms.JmsFactoryFactory;

please email it to - mugadavs@gmail.com
thanks,

Anonymous said...

nintеndo wii is pеrfect :D
Also see my website :: wiiutillbehor.se

Anonymous said...

Lättaгe döljer en PlayЅtation
4 ѕin visdom än en tοk ѕin vanvеtt
.
Here is my web site - playstation portable jap

sudheer said...

Snehanshu/Ashif

Could you please email me the jar files at findsudheer@gmail.com
for these two class files

1)import com.ibm.msg.client.jms.JmsConnectionFactory;
2)import com.ibm.msg.client.jms.JmsFactoryFactory

Thanks
Sudheer

Unknown said...

Hi Snehanshu,

could you please provide jar files for the following ?

1)import com.ibm.msg.client.jms.JmsConnectionFactory;
2)import com.ibm.msg.client.jms.JmsFactoryFactory;

please email it to - dfernandezyopla@gmail.com
thanksfor your time, hope you can help me

Majid said...

Hi,
Thanks for this tutorial, but this code send only the name of the file as a message not the content, is there any way on how to tweak it to send the content ?
thanks, your help is appreciated.

पराग मांडे said...

In above code, is the connection to MQ is using binding method or client method?

Total Pageviews