Exemple #1
0
        /// <summary>
        /// Receive message only and non-blocking queue
        /// </summary>
        /// <param name="consumer"></param>
        /// <returns></returns>
        public String ConvertMessageObjectToString(IMessage msg)
        {
            String msgStr = null;

            if (msg != null)
            {
                if (msg is ITextMessage)
                {
                    msgStr = ((ITextMessage)msg).Text;
                    /// NOTE: there are some null-terminated characters from ES and CCS, so need to convert to whitespaces; otherwise, the message is truncated
                    //msgStr = ((ITextMessage)msg).Text.Replace("\0", " ");
                    //byte[] bytes = Encoding.UTF8.GetBytes(((ITextMessage)msg).Text);
                    //msgStr = "text <" + Encoding.UTF8.GetString(bytes) + ">";
                    //msgStr = msg.GetIntProperty(XMSC.JMS_IBM_CHARACTER_SET) + " " + msg.GetIntProperty(XMSC.JMS_IBM_ENCODING) + " " + ((ITextMessage)msg).Text;
                }
                else if (msg is IBytesMessage)
                {
                    int    num   = (int)((IBytesMessage)msg).BodyLength;
                    byte[] bytes = new byte[num];
                    ((IBytesMessage)msg).ReadBytes(bytes);
                    msgStr = Encoding.UTF8.GetString(bytes);
                }
                msg.Acknowledge();
                msg.ClearBody();
            }
            return(msgStr);
        }
Exemple #2
0
        /// <summary>
        /// less efficient version of receive message
        /// </summary>
        /// <returns></returns>
        public String ReceiveMessage()
        {
            String             msgStr      = "";
            IConnectionFactory connFactory = CreateMQConnectionFactoryWMQ();

            using (IConnection conn = CreateMQConnection(connFactory))  // ensure Dispose() even with exceptions, similar to try-catch
            {
                using (ISession sess = CreateMQSession(conn))
                {
                    using (IDestination dest = CreateMQDestination(sess, _consumerQueue))
                    {
                        using (IMessageConsumer consumer = CreateMQConsumer(sess, dest))
                        {
                            //using (receiveCompleteEvent = new System.Threading.AutoResetEvent(false)) // not using ASYNC way
                            //{
                            //    MessageListener msgLsnr = new MessageListener(handleMessage);
                            //    consumer.MessageListener = msgLsnr;
                            //    conn.Start();
                            //}

                            conn.Start();
                            IMessage msg = null;
                            msg = consumer.ReceiveNoWait(); // non-blocking
                            //Console.WriteLine(msg) ;
                            if (msg != null)
                            {
                                if (msg is ITextMessage)
                                {
                                    msg.SetIntProperty(XMSC.JMS_IBM_CHARACTER_SET, XMSC.CCSID_UTF8);
                                    msg.SetIntProperty(XMSC.JMS_IBM_ENCODING, XMSC.WMQ_ENCODING_NATIVE);
                                    msgStr = ((ITextMessage)msg).Text.Replace("\0", " ");     // replace null-terminated characters to empty; otherwise, cannot display
                                    // tbxOutput.AppendText(((ITextMessage)msg).Text);
                                }
                                else if (msg is IBytesMessage)
                                {
                                    byte[] bytes = null;
                                    int    num   = ((IBytesMessage)msg).ReadBytes(bytes);
                                    msgStr = Encoding.UTF8.GetString(bytes);
                                    // tbxOutput.AppendText(Encoding.UTF8.GetString(bytes));
                                }

                                msg.ClearBody();
                            }
                            else
                            {
                                msgStr = "nothing received.\n";
                            }
                            consumer.Close();
                            consumer.Dispose();
                        }
                        dest.Dispose();
                    }
                    sess.Close();
                    sess.Dispose();
                }
                conn.Stop();
                conn.Close();
                conn.Dispose();
            } // close connection
            return(msgStr);
        }