public void TestReadChar()
        {
            BytesMessage msg = new BytesMessage();

            msg.WriteChar('a');
            msg.Reset();
            Assert.IsTrue(msg.ReadChar() == 'a');
        }
示例#2
0
    private void Run()
    {
        Message msg = null;

        Console.WriteLine("Subscribing to destination: " + name + "\n");

        ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(serverUrl);

        // create the connection
        connection = factory.CreateConnection(userName, password);

        // create the session
        session = connection.CreateSession(false, ackMode);

        // set the exception listener
        connection.ExceptionListener = this;

        // create the destination
        if (useTopic)
        {
            destination = session.CreateTopic(name);
        }
        else
        {
            destination = session.CreateQueue(name);
        }

        // create the consumer
        msgConsumer = session.CreateConsumer(destination);

        // start the connection
        connection.Start();

        // read messages
        while (true)
        {
            // receive the message
            msg = msgConsumer.Receive();
            if (msg == null)
            {
                break;
            }

            if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
                ackMode == Session.EXPLICIT_CLIENT_ACKNOWLEDGE ||
                ackMode == Session.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
            {
                msg.Acknowledge();
            }

            Console.WriteLine("Received message: " + msg);
            if (msg is BytesMessage)
            {
                BytesMessage bm = (BytesMessage)msg;
                Console.WriteLine(bm.ReadBoolean());
                Console.WriteLine(bm.ReadChar());
                Console.WriteLine(bm.ReadShort());
                Console.WriteLine(bm.ReadInt());
                Console.WriteLine(bm.ReadLong());
                Console.WriteLine(bm.ReadFloat());
                Console.WriteLine(bm.ReadDouble());
            }
        }

        // close the connection
        connection.Close();
    }
        public void TestWriteOnlyBody()
        {
            BytesMessage message = new BytesMessage();

            message.ClearBody();

            try
            {
                message.WriteBoolean(true);
                message.WriteByte((byte)1);
                message.WriteBytes(new byte[1]);
                message.WriteBytes(new byte[3], 0, 2);
                message.WriteChar('a');
                message.WriteDouble(1.5);
                message.WriteSingle((float)1.5);
                message.WriteInt32(1);
                message.WriteInt64(1);
                message.WriteObject("stringobj");
                message.WriteInt16((short)1);
                message.WriteString("utfstring");
            }
            catch (MessageNotWriteableException)
            {
                Assert.Fail("Should be writeable");
            }

            try
            {
                message.ReadBoolean();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadByte();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadBytes(new byte[1]);
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadBytes(new byte[2], 2);
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadChar();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadDouble();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadSingle();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt32();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt64();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadString();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt16();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadString();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }
        }
        public void Consumer()
        {
            int ackMode = Session.AUTO_ACKNOWLEDGE;

            TIBCO.EMS.Message msg = null;

            while (true)
            {
                try
                {
                    // receive the message
                    msg = msgConsumer.Receive();
                }
                catch (EMSException EMSex)
                {
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + EMSex.Message; });
                    logger.Error(EMSex.Message);
                }
                if (msg == null)
                {
                    break;
                }

                if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
                    ackMode == Session.EXPLICIT_CLIENT_ACKNOWLEDGE ||
                    ackMode == Session.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
                {
                    msg.Acknowledge();
                }

                LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\nReceived message: " + msg; });
                logger.Info("Received message: " + msg);

                if (msg is TextMessage)
                {
                    TextMessage tm = (TextMessage)msg;
                    //ReceveMsgTextBox.Text = tm.Text;
                    ReceveMsgTextBox.Invoke((MethodInvoker) delegate() { ReceveMsgTextBox.Text = tm.Text; });
                    logger.Info(tm.Text);
                }
                if (msg is BytesMessage)
                {
                    BytesMessage bm = (BytesMessage)msg;
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadBoolean(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadChar(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadShort(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadInt(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadLong(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadFloat(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadDouble(); });
                }

                if (msg is MapMessage)
                {
                    MapMessage mm       = (MapMessage)msg;
                    var        mapNames = mm.GetMapNames();
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.ToString(); });

                    /*   foreach (String name in mapNames)
                     * {
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetBoolean(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetByte(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetBytes(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetChar(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetShort(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetInt(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetLong(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetFloat(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetDouble(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetString(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetObject(name); });
                     * }*/
                }
            }
        }