import com.ibm.mq.* ; // Include the WebSphere MQ classes for Java package // Based on "Using Java", csqzaw12.pdf, // part 2, "Programming with WebSphere MQ base Java" // chapters 6, 7, 8 and 9, pages 63 thru 198. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // WebSphere MQ classes for Java is a set of Java classes that enable // Java applets and applications to interact with WebSphere MQ. // The following classes are provided : // v MQChannelDefinition // v MQChannelExit // v MQDistributionList // v MQDistributionListItem // v MQEnvironment // v MQException // v MQGetMessageOptions // v MQManagedObject // v MQMessage // v MQMessageTracker // v MQPoolServices // v MQPoolServicesEvent // v MQPoolToken // v MQPutMessageOptions // v MQProcess // v MQQueue // v MQQueueManager // v MQSimpleConnectionManagerThe // To erase "old" messages in "ANGELQ" queue, use "amqsget ANGELQ" ! // MQ RCs and RSs : Messages, chapter 8. // Versions : // 1.0 - init, 19102004, 19h. // 1.1 - PUT works. // 1.2 - remove GET. // 1.3 - set UserId ! // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public class Qmsag { private static String szProducto = "JavaPut" ; private static String szVersion = "Version 1.3" ; private static String szAutor = "Sebastia Altemir" ; private static String qManager = "QM_sag_mqwf" ; // define name of queue manager to connect to. private static MQQueueManager qMgr ; // define a queue manager object. private static String qData = "ANGELQ" ; // define name of queue to connect to. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static void main ( String args [] ) { System.out.println ( "Hola. Esto es ["+ szProducto + "], autor [" + szAutor + "], version [" + szVersion + "]." ) ; MQSample ( ) ; } ; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static void MQSample ( ) { int iGetBack = 0 ; try { // Create a connection to the queue manager System.out.println ( "Create a Connection to the Queue Manager [" + qManager + "]." ) ; qMgr = new MQQueueManager ( qManager ) ; // Set up the options on the queue we wish to open... // Note. All WebSphere MQ Options are prefixed with MQC in Java. // int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT ; int openOptions = MQC.MQOO_SET_ALL_CONTEXT | MQC.MQOO_OUTPUT ; // allow to set UserID ! // Now specify the queue that we wish to open, and the open options... System.out.println ( "We shall use Queue [" + qData + "]. Open options [" + openOptions + "]." ) ; MQQueue qSAG = qMgr.accessQueue ( qData, openOptions ) ; // Define a simple WebSphere MQ message, and write some text in UTF format.. MQMessage hello_world = new MQMessage ( ) ; hello_world.writeUTF ( "Hola, Angel ! Usando Java !!! Producto [" + szProducto + "], version [" + szVersion + "]." ) ; // set some MD fields ... // hello_world.applicationIdData = "AppIdSAG" ; // hello_world.applicationOriginData = "OriginSAG" ; // hello_world.backoutCount = 1 ; hello_world.format = MQC.MQFMT_STRING ; hello_world.messageType = MQC.MQMT_REQUEST ; hello_world.persistence = MQC.MQPER_PERSISTENT ; // hello_world.priority = 3 ; hello_world.replyToQueueManagerName = "QMsag88toReplyTo" ; hello_world.replyToQueueName = "Qu44toReplyTo" ; // hello_world.report = MQC.MQFB_COA + MQC.MQFB_COD ; // RC = 2, RS = 2061 to "put". Report field error. hello_world.userId = "angelito" ; // specify the message options... MQPutMessageOptions pmo = new MQPutMessageOptions ( ) ; // pmo.options = MQC.MQPMO_NO_CONTEXT ; // => " --blank -- " // pmo.options = MQC.MQPMO_NONE ; // => "Administrato" // pmo.options = MQC.MQPMO_PASS_IDENTITY_CONTEXT ; // RC = 2, RS = 2097 to "put" - PASS_CONTEXT "Put" but not "Open". pmo.options = MQC.MQPMO_SET_ALL_CONTEXT ; // OK with MQOO_SET_ALL_CONTEXT ! System.out.println ( "El UserID de este mensaje (antes de PUT) es [" + hello_world.userId + "]." ) ; // put the message on the queue System.out.println ( "Put the message on the queue." ) ; qSAG.put ( hello_world, pmo ) ; System.out.println ( "El UserID de este mensaje (despues de PUT) es [" + hello_world.userId + "]." ) ; if ( iGetBack == 1 ) { // get the message back again... // First define a WebSphere MQ message buffer to receive the message into.. MQMessage retrievedMessage = new MQMessage ( ) ; retrievedMessage.messageId = hello_world.messageId ; System.out.println ( "El UserID de este mensaje (3) es [" + hello_world.userId + "]." ) ; // Set the get message options... MQGetMessageOptions gmo = new MQGetMessageOptions ( ) ; // accept the defaults, same as MQGMO_DEFAULT // get the message off the queue... System.out.println ( "Get the message back again." ) ; qSAG.get ( retrievedMessage, gmo ) ; // And prove we have the message by displaying the UTF message text String msgText = retrievedMessage.readUTF ( ) ; System.out.println ( "The message is : [" + msgText + "]." ) ; } ; // Close the queue... System.out.println ( "Close the queue." ) ; qSAG.close ( ) ; // Disconnect from the queue manager System.out.println ( "Disconnect from the queue manager." ) ; qMgr.disconnect ( ) ; } // If an error has occurred in the above, try to identify what went wrong // Was it a WebSphere MQ error? catch ( MQException ex ) { System.out.println ( "--- WebSphere MQ error : CC " + ex.completionCode + " RC " + ex.reasonCode ) ; } // Was it a Java buffer space error? catch ( java.io.IOException ex ) { System.out.println ( "--- Error whilst writing to the message buffer: " + ex ) ; } } ; // MQSample } ; // Qmsag