Get.java

 


/***********************************************************************
*
*    Get - Get messages from queue.
*    Eric Olson MQSoftware, October, 2001
*                    
*    Java exercise for MQSoftware Applications Bootcamp.
*
*    Get retrieves messages from a user specified queue and displays 
*        them on standard output (screen).  The messages are assumed to 
*        be short text strings.  Messages will be truncated if they are 
*        larger than the buffer size.
*
*    Added student comments, proper tabs and proper capitalization.    Matthew Neis
**********************************************************************/

//      Standard Java imports.
import java.io.*;

//      Imports for MQSeries.
import com.ibm.mq.*;

//        Main program.

public class Get
{                                                                    
    // start of Get class
    public static void main (String arguments[])
    {
        // MQSeries objects
        MQQueueManager           qMgr = null;                        // Queue Manager
        MQQueue                  queue = null;                       // Queue
        MQGetMessageOptions      gmo = new MQGetMessageOptions();    // GetMessageOptions Structure
        MQMessage                msg = new MQMessage();              // Message
        String                   inputQueue = "";                    // Name of the output queue
        String                   prompt;                             // Prompt to be displayed for user entry
        int                      openOptions;                        // Reasons for opening the queue
        int                      OpenCode = 0;                       // Completion code from the MQOPEN call
        int                      msglen = 0;                         // Length of the message
        String                   qMgrName = "";                      // queue manager name - default
        
        System.out.println("Get started.");

        // Connect to the queue manager

        try
        {                                                            
            // CONNECT TO THE DEFAULT LOCAL QUEUE MANAGER
            qMgr = new MQQueueManager(qMgrName);        
        }
        catch  MQException(ex)
        {
            System.out.println("\nMQCONN  was unsuccessful.\n\tCompCode= " + ex.completionCode + " ConReason = " + ex.reasonCode);
            //  return(ex.reasonCode);
        }
        
        // Get input queue name from keyboard and display it.

        KeyboardRead kr = new KeyboardRead();
        
        prompt = "Enter the input queue name:";
        inputQueue = kr.stringRead(prompt);

        System.out.println("\nInput queue name is " + inputQueue);

        // Set queue name, set open options,
        // and open the server request queue.
        
        openOptions =  MQC.MQOO_INPUT_EXCLUSIVE |            // Set the OpenOptions to open the queue for output
                        MQC.MQOO_FAIL_IF_QUIESCING;          // and to acknowledge queue manager shutdown request

                                                                    
        try
        {


        // INSERT THE OPEN CALL HERE    
            queue = qMgr.accessQueue(inputQueue,             // od.ObjectName
                                        openOptions,         // OpenOptions
                                        null,                // ObjectQMgrName
                                        null,                // od.DynamicQName
                                        null);               // od.AlternateUserId
        
        
        
        }
        // Check for problems.  If there is a problem, display the reason.
        // Note that OpenCode is preserved so MQCLOSE will be skipped if OPEN fails.
        catch  MQException(ex)
        {
            System.out.println("\nMQOPEN was unsuccessful.\n\tOpenCode= " + ex.completionCode + "\tReason = " + ex.reasonCode);

            OpenCode = ex.completionCode;     // Preserve the Completion code to skip 
        }                                     // the close, if open failed


        //  Main read message / display text loop.

        gmo.options = MQC.MQGMO_WAIT |
                    MQC.MQGMO_ACCEPT_TRUNCATED_MSG |
                    MQC.MQGMO_CONVERT |                     // INSERT THE SPECIFIED GET MESSAGE
                    MQC.MQGMO_FAIL_IF_QUIESCING |           // OPTIONS HERE TO CONTROL THE
                    MQC.MQGMO_NO_SYNCPOINT;                 // OPERATION OF THE MQGET CALL



        gmo.waitInterval = 5000;                            //SET WAITINTERVAL HERE
                                                            //TO WAIT ABOUT 5 SECONDS



        while (OpenCode != MQException.MQCC_FAILED)         // Unless open failed and until put fails    
        {                                                   // main get loop


            //CLEAR MSGID AND CORRELID HERE TO GET ANY MESSAGE AVAILABLE
            msg.messageId = MQC.MQMI_NONE;                                
            msg.correlationId = MQC.MQCI_NONE;                            
          

            try
            {


            // INSERT GET CALL HERE
                queue.get(msg, gmo);                                


            }
            catch  MQException(ex)
            {
                System.out.println("\nMQGET was unsuccessful.\n\tCompCode= " +
                    ex.completionCode + "\tReason = " + ex.reasonCode);
                OpenCode = ex.completionCode;
            }  
        
            if (OpenCode != MQException.MQCC_FAILED)                
            {
                try
                {
                    System.out.println("Message [" + msg.readLine() + "]");// display gotten message
                }
                catch  IOException(ex)
                {
                    System.out.println("Exception: " + ex.getMessage());
                    break;
                }

            }


                //SET SHORTER WAITINTERVAL HERE FOR SUBSEQUENT MESSAGES
                gmo.waitInterval = 500;        


         }      //end of main get loop
        
        //      If MQOPEN was successful, Close input queue.

        if (OpenCode != MQException.MQCC_FAILED)
        {
        
            queue.closeOptions = MQC.MQCO_NONE;             //do not delete permdyn queues

            try
            {

            // CLOSE THE QUEUE OBJECT HERE
                queue.close( );                


            }
            catch  MQException(ex)
            {
                System.out.println("\nMQCLOSE was unsuccessful.\n\tCompCode= " +
                    ex.completionCode + "\tReason = " + ex.reasonCode);
            }
            //   Disconnect from MQM if not already connected (the            
            //   MQQueueManager object handles this situation automatically) 
        }
        try
        {

            // DISCONNECT FROM THE QMGR HERE
            qMgr.disconnect( );                            


        }
        catch  MQException(ex)
        {
            System.out.println("\nMQDISC was unsuccessful.\n\tCompCode= " +
                ex.completionCode + "\tReason = " + ex.reasonCode);
        }
        System.out.println("\nGet ended.");
//        return(0);
    }    // end of Get
}