I use the Java Message Service Framework to post
and receive messages from an MQ Server running on our iSeries.
Here's a sample program that might help you to get
started. For more information, check out Suns website.
//************************************************************************* // // Program: mqClient // // Purpose: Connect to a remote MQ Series QManager and retrieve messages // // Paramaters: Host IP Address or Name; QManager Name; Channel; QName // //************************************************************************** import com.ibm.mq.*; import com.ibm.mqjms.*; import java.io.*;
public class mqClient
{
String
msgResponse = "";
String qName = ""; String qManager = ""; String qChannel = ""; String qHostIP = ""; Queue ioQueue = null; QueueSession session = null; QueueConnection connection = null; QueueConnectionFactory factory = null; String msgResponse = ""; //***********************************************************************
// main() method //*********************************************************************** public void main(String args[]) { buildJNDIFactory(args[0], args[1], args[2]); startConnection(); obtainSession(); msgResponse = getMessages(); closeResources(); } //*********************************************************************** // Create a new JNDI Connection Factory //*********************************************************************** public void buildJNDIFactory(String Host, String qManager, String Channel) { factory = new
com.ibm.mq.jms.MQQueueConnectionFactory();
factory= new MQQueueConnectionFactory(); factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP); factory.setQueueManager(qManager); factory.setHostName(qHostIP); factory.setChannel(qChannel); } //***********************************************************************
// Create a connection //*********************************************************************** public void startConnection() { connection = factory.createQueueConnection(); connection.start; } //*************************************************************************
// Obtain Session //************************************************************************* public void obtainSession() { boolean transacted = false; session = connection.createQueueSession(transacted,
Session.AUTO_ACKNOWLEDGE);
ioQueue = session.createQueue(qName); // Set Queue Receiver QueueReceiver queueReceiver = session.createReceiver(ioQueue); }
//********************************************************************************
// Get Messages off of Queue //******************************************************************************** public String getMessages() { Message inMessage = queueReceiver.receive(1000); // Check to see if the receive call has actually returned
a
// message. If it hasn't, report this and throw an exception... if( inMessage == null ) { System.out.println( "No Messages on Queue"); throw new JMSException("Failed to retrieve a message"); } System.out.println( "\n" + "Got message"+": "+inMessage);
// Check that the message received (a) is of
the correct type,
// and (b) contains the same text as the one sent, reporting the // result of these two checks if( inMessage instanceof TextMessage ) { // Extract the message content with getText()
String replyString = ((TextMessage) inMessage).getText(); } return replyString; }
//********************************************************************************
// Close Resources //******************************************************************************** public void closeResources() { // Closing QueueReceiver System.out.println("Closing QueueReceiver"); queueReceiver.close(); // Closing
QueueSesssion.
System.out.println("Closing Session"); session.close(); session = null; // Closing
QueueConnection.
System.out.println("Closing Connection"); connection.close(); connection = null; } }
Shannon O'Donnell
|