示例#1
0
        public string ReadOneMensage()
        {
            MQQueueManager qMgr = new MQQueueManager(IbmConection.DbConection[5], _queueManagerProperties);

            // Agora especifique a fila que queremos abrir e as opções de abertura
            MQQueue fila = qMgr.AccessQueue(IbmConection.DbConection[6], MQC.MQQA_GET_ALLOWED + MQC.MQQA_GET_INHIBITED + MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE);

            MQMessage retrievedMessage = new MQMessage();

            // Defina as opções de obtenção de mensagens
            MQGetMessageOptions gmo = new MQGetMessageOptions(); // aceita os padrões
                                                                 // mesmo que MQGMO_DEFAULT

            string msgText = "";

            if (fila.CurrentDepth > 0)
            {
                // Tire a mensagem da fila
                fila.Get(retrievedMessage, gmo);

                msgText = retrievedMessage.ReadUTF();

                return(msgText);
            }
            else
            {
                return(msgText);
            }
        }
示例#2
0
        /// <summary>
        /// Receive a reply message
        /// </summary>
        /// <returns></returns>
        public String ReceiveMessage(byte[] messageId)
        {
            String    retMsg = "";
            MQMessage mqMsg  = null;

            try
            {
                mqMsg = new MQMessage();
                mqMsg.CorrelationId = messageId;
                MQGetMessageOptions gmo = new MQGetMessageOptions();
                gmo.MatchOptions |= MQC.MQMO_MATCH_CORREL_ID;
                gmo.WaitInterval  = 3000;
                gmo.Options      |= MQC.MQGMO_WAIT;

                replyQ.Get(mqMsg, gmo);
                retMsg = mqMsg.ReadUTF();
            }
            catch (MQException ex)
            {
                if (ex.Reason == 2033)
                {
                    retMsg = "";
                }
                else
                {
                    throw ex;
                }
            }

            return(retMsg);
        }
示例#3
0
        private void PutAndGetMqSeries()
        {
            Hashtable connectionProperties = init(connectionType);

            // Create a connection to the queue manager using the connection
            // properties just defined
            MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);

            // Set up the options on the queue we want to open
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;

            // Now specify the queue that we want to open,and the open options
            MQQueue system_default_local_queue =
                qMgr.AccessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE", openOptions);

            // Define an IBM MQ message, writing some text in UTF format
            MQMessage hello_world = new MQMessage();

            hello_world.WriteUTF("Hello World!");

            // Specify the message options
            MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,

            // same as MQPMO_DEFAULT

            // Put the message on the queue
            system_default_local_queue.Put(hello_world, pmo);

            // Get the message back again

            // First define an IBM MQ message buffer to receive the message
            MQMessage retrievedMessage = new MQMessage();

            retrievedMessage.MessageId = hello_world.MessageId;

            // Set the get message options
            MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults

            //same as MQGMO_DEFAULT

            // Get the message off the queue
            system_default_local_queue.Get(retrievedMessage, gmo);

            // Prove we have the message by displaying the UTF message text
            String msgText = retrievedMessage.ReadUTF();

            Console.WriteLine("The message is: {0}", msgText);

            // Close the queue
            system_default_local_queue.Close();

            // Disconnect from the queue manager
            qMgr.Disconnect();
        }
示例#4
0
        /// <summary>
        /// Run method - a big while loop that handle requests from web role
        /// </summary>
        public override void Run()
        {
            // Diagnostics for debugging purpose
            Trace.TraceInformation("mqSqlWorker-Run entry point called");

            while (true)
            {
                // We sleep for a second before reading next request.
                Thread.Sleep(1000);

                // We are good to go as we all setup.
                if (bConnInitialized)
                {
                    //Get a message from RequestQ and Update database
                    TransactionScope ts = null;

                    try
                    {
                        // create a new transaction scope
                        ts = new TransactionScope();

                        // Get a message under a transaction
                        MQMessage           mqMsg = new MQMessage();
                        MQGetMessageOptions gmo   = new MQGetMessageOptions();
                        gmo.WaitInterval = MQC.MQWI_UNLIMITED;
                        gmo.Options     |= MQC.MQGMO_WAIT | MQC.MQGMO_SYNCPOINT;

                        _mqRequestQueue.Get(mqMsg, gmo);
                        String msgData = mqMsg.ReadUTF();
                        Trace.TraceInformation("Incoming Data: " + msgData);

                        // The incoming data will be in two parts. One will be the command
                        // to execute and the second part will be data for the command. The
                        // : is the separator
                        String[] cmdPart = msgData.Split('=');

                        // Check if we have the request in correct format.
                        if (cmdPart.Length == 2)
                        {
                            // Yes, then split the first part of the command.
                            //String[] cmdPart = cmdWithData[0].Split('=');

                            // So check what command we have got
                            if (cmdPart[0].Equals(Constants.CMD_COMMAND_KEY) == true)
                            {
                                // Query College
                                if (cmdPart[1].Equals(Constants.CMD_QUERY_COLLEGE) == true)
                                {
                                    // We need to send back query list
                                    String jsonString = getRecordsFromDB();
                                    sendReply(jsonString, mqMsg.MessageId);
                                    Trace.TraceInformation("Reply for Query College sent" + ": " + jsonString);
                                }
                                else
                                {
                                    // Unrecognized request. Just trace out.
                                    Trace.TraceInformation("Unrecognized request");
                                }
                            }
                        }
                        ts.Complete();
                    }
                    catch (MQException mqex)
                    {
                        if (mqex.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE)
                        {
                            Trace.TraceInformation("Failed transaction: " + mqex.ToString());
                        }
                        else if (mqex.ReasonCode == MQC.MQRC_CONNECTION_BROKEN)
                        {
                            Trace.TraceInformation("Connection to queue manager broken. Reconnecting.");
                            bConnInitialized = false;
                            Initialize();
                        }
                    }
                    catch (System.Transactions.TransactionAbortedException tae)
                    {
                        Trace.TraceInformation("Failed transaction: " + tae);
                    }
                    catch (System.TimeoutException te)
                    {
                        Trace.TraceInformation("Failed transaction: " + te);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceInformation("Failed transaction: " + ex);
                    }

                    // Dispose Transaction scope
                    if (ts != null)
                    {
                        ts.Dispose();
                    }
                }
                else
                {
                    // If we have failied initialize during start up, then try now.
                    Initialize();
                }
            }
        }