コード例 #1
0
        public void TestReset()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            try
            {
                message.WriteDouble(24.5);
                message.WriteInt64(311);
            }
            catch (MessageNotWriteableException)
            {
                Assert.Fail("should be writeable");
            }

            message.Reset();

            try {
                Assert.IsTrue(message.ReadOnlyBody);
                Assert.AreEqual(message.ReadDouble(), 24.5, 0);
                Assert.AreEqual(message.ReadInt64(), 311);
            }
            catch (MessageNotReadableException)
            {
                Assert.Fail("should be readable");
            }

            try
            {
                message.WriteInt32(33);
                Assert.Fail("should throw exception");
            }
            catch (MessageNotWriteableException)
            {
            }
        }
コード例 #2
0
        protected override IBytesMessage DoCreateBytesMessage()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            message.Connection = this.connection;
            return(message);
        }
コード例 #3
0
        public void Initialize()
        {
            try
            {
                IConnectionFactory connectionFactory = new ConnectionFactory(URI);
                IConnection        _connection       = connectionFactory.CreateConnection();
                _connection.Start();
                ISession     _session = _connection.CreateSession();
                IDestination dest     = _session.GetDestination(DESTINATIONS[2]);
                using (IMessageConsumer consumer = _session.CreateConsumer(dest))
                {
                    Console.WriteLine("Listener started.");
                    Console.WriteLine("Listener created.");
                    IMessage message;
                    while (true)
                    {
                        message = consumer.Receive(TimeSpan.FromMilliseconds(200));

                        if (message != null)
                        {
                            ActiveMQBytesMessage bm = message as ActiveMQBytesMessage;
                            Console.WriteLine(message.ToString());
                            Console.WriteLine("CONTENT: " + BitConverter.ToString(bm.Content));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("Press <ENTER> to exit.");
                Console.Read();
            }
        }
コード例 #4
0
        public void TestWriteObject()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            try
            {
                msg.WriteObject("fred");
                msg.WriteObject((Boolean)true);
                msg.WriteObject((Char)'q');
                msg.WriteObject((Byte)((byte)1));
                msg.WriteObject((Int16)((short)3));
                msg.WriteObject((Int32)3);
                msg.WriteObject((Int64)300L);
                msg.WriteObject((Single)3.3f);
                msg.WriteObject((Double)3.3);
                msg.WriteObject((Object) new byte[3]);
            }
            catch (MessageFormatException)
            {
                Assert.Fail("objectified primitives should be allowed");
            }

            try
            {
                msg.WriteObject(new Object());
                Assert.Fail("only objectified primitives are allowed");
            }
            catch (MessageFormatException)
            {
            }
        }
コード例 #5
0
        public void ReceiveMessage(IMessage msg)
        {
            if (this.OnMessageReceived != null)
            {
                String message = null;
                if (msg.GetType() == typeof(ActiveMQTextMessage))
                {
                    message = (msg as ActiveMQTextMessage).Text;
                }
                else if (msg.GetType() == typeof(ActiveMQBytesMessage))
                {
                    ActiveMQBytesMessage bytesMessage = (ActiveMQBytesMessage)msg;
                    message = Encoding.UTF8.GetString(bytesMessage.Content);
                }
                else
                {
                    throw new NMSException("Message could not be parsed ");
                }

                MessageEvent messageEvent = new MessageEvent(msg.Properties["destination"].ToString(), message, msg.NMSMessageId, msg.NMSTimestamp);
                foreach (String key in msg.Properties.Keys)
                {
                    messageEvent.setObjectProperty(key, msg.Properties[key]);
                }
                this.OnMessageReceived(messageEvent, messageEvent.getEventType());
            }
        }
コード例 #6
0
        public IBytesMessage CreateBytesMessage(byte[] body)
        {
            ActiveMQBytesMessage answer = new ActiveMQBytesMessage();

            answer.Content = body;
            return(ConfigureMessage(answer) as IBytesMessage);
        }
コード例 #7
0
        public void TestClearBody()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            message.ClearBody();
            Assert.IsFalse(message.ReadOnlyBody);
        }
コード例 #8
0
        public void TestReadShort()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteInt16((short)3000);
            msg.Reset();
            Assert.IsTrue(msg.ReadInt16() == 3000);
        }
コード例 #9
0
        public void TestReadByte()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteByte((byte)2);
            msg.Reset();
            Assert.IsTrue(msg.ReadByte() == 2);
        }
コード例 #10
0
        public void TestReadBoolean()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteBoolean(true);
            msg.Reset();
            Assert.IsTrue(msg.ReadBoolean());
        }
コード例 #11
0
        //
        // Write the booleans that this object uses to a BooleanStream
        //
        public override int TightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs)
        {
            ActiveMQBytesMessage info = (ActiveMQBytesMessage)o;

            int rc = base.TightMarshal1(wireFormat, info, bs);

            return(rc + 0);
        }
コード例 #12
0
        public void TestReadDouble()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteDouble(3.3d);
            msg.Reset();
            Assert.IsTrue(msg.ReadDouble() == 3.3d);
        }
コード例 #13
0
        public void TestReadFloat()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteSingle(3.3f);
            msg.Reset();
            Assert.IsTrue(msg.ReadSingle() == 3.3f);
        }
コード例 #14
0
        public void TestReadLong()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteInt64(3000);
            msg.Reset();
            Assert.IsTrue(msg.ReadInt64() == 3000);
        }
コード例 #15
0
        public void TestReadChar()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            msg.WriteChar('a');
            msg.Reset();
            Assert.IsTrue(msg.ReadChar() == 'a');
        }
コード例 #16
0
        public void TestReadString()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
            string str = "this is a test";

            msg.WriteString(str);
            msg.Reset();
            Assert.IsTrue(msg.ReadString() == str);
        }
コード例 #17
0
        public void TestGetBodyLength()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
            int len = 10;

            for (int i = 0; i < len; i++)
            {
                msg.WriteInt64(5L);
            }

            msg.Reset();
            Assert.IsTrue(msg.BodyLength == (len * 8));
        }
コード例 #18
0
        public void TestReadBytesbyteArray()
        {
            ActiveMQBytesMessage msg = new ActiveMQBytesMessage();

            byte[] data = new byte[50];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)i;
            }
            msg.WriteBytes(data);
            msg.Reset();
            byte[] test = new byte[data.Length];
            msg.ReadBytes(test);
            for (int i = 0; i < test.Length; i++)
            {
                Assert.IsTrue(test[i] == i);
            }
        }
コード例 #19
0
        public void TestClearBody()
        {
            ActiveMQBytesMessage bytesMessage = new ActiveMQBytesMessage();

            try {
                bytesMessage.WriteInt32(1);
                bytesMessage.ClearBody();
                Assert.IsFalse(bytesMessage.ReadOnlyBody);
                bytesMessage.WriteInt32(1);
                bytesMessage.ReadInt32();
            }
            catch (MessageNotReadableException)
            {
            }
            catch (MessageNotWriteableException)
            {
                Assert.IsTrue(false);
            }
        }
コード例 #20
0
        public void TestCommand()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            // Test that a BytesMessage is created in WriteOnly mode.
            try
            {
                byte[] content = message.Content;
                content.SetValue(0, 0);
                Assert.Fail("Should have thrown an exception");
            }
            catch
            {
            }

            Assert.IsTrue(!message.ReadOnlyBody);
            Assert.IsTrue(!message.ReadOnlyProperties);

            message.Reset();

            Assert.IsNull(message.Content);
            Assert.IsTrue(message.ReadOnlyBody);
        }
コード例 #21
0
 private void ConsumerOnListener(QuqueName ququeName, IMessage message)
 {
     try
     {
         string msgContext = "";
         if (message is ActiveMQBytesMessage)
         {
             bool isZipCompress = false;
             if (message.Properties.Contains(MqConsts.IsZipCompress))
             {
                 isZipCompress = message.Properties.GetBool(MqConsts.IsZipCompress);
             }
             ActiveMQBytesMessage msg = (ActiveMQBytesMessage)message;
             if (isZipCompress)
             {
                 msgContext = StringZipHelper.GZipDecompress(msg.Content);
             }
             else
             {
                 msgContext = System.Text.Encoding.UTF8.GetString(msg.Content);
             }
         }
         else if (message is ActiveMQTextMessage)
         {
             ActiveMQTextMessage msg = (ActiveMQTextMessage)message;
             msgContext = msg.Text;
         }
         else
         {
             _logger.Error("消费者监听事件处理错误.暂时只接受ActiveMQBytesMessage、ActiveMQTextMessage消息体");
         }
         var clientId = GetClientId(message);
         if (clientId == Guid.Empty || clientId == null)
         {
             _logger.Error(string.Format("消息无效,没有匹配的门店与命令号 "));
             return;
         }
         var            msgType     = message.NMSType;
         NMSMessageType messageType = NMSMessageType.None;
         if (!string.IsNullOrEmpty(msgType))
         {
             messageType = Utils.GetEnum <NMSMessageType>(msgType);
         }
         MqCommandInfo commandInfo = new MqCommandInfo()
         {
             StoreId      = clientId.Value,
             Body         = msgContext,
             Length       = msgContext.Length,
             MessageType  = messageType,
             NMSMessageId = message.NMSMessageId,
             MqInstancId  = _configInfo.MqInstanceId,
             ClientId     = this.ClientId,
             Properties   = new Dictionary <string, string>()
         };
         if (message.Properties != null)
         {
             foreach (var key in message.Properties.Keys)
             {
                 if (key == null)
                 {
                     continue;
                 }
                 var keystr = key.ToString();
                 if (!commandInfo.Properties.ContainsKey(keystr))
                 {
                     commandInfo.Properties.Add(keystr, message.Properties.GetString(keystr));
                 }
             }
         }
         OnQuqueDispatch(ququeName, commandInfo);
         if (commandInfo.ExcuteCount == 0)
         {
             _logger.Error(string.Format("消息未找到处理程序 类型: {0}大小: {1}", commandInfo.MessageType, commandInfo.Length));
         }
     }
     catch (Exception ex)
     {
         _logger.Error(ex, "消费者监听事件处理错误.ConsumerOnListener");
     }
 }
コード例 #22
0
        protected virtual Command ReadMessage(string command, IDictionary headers, byte[] content)
        {
            ActiveMQMessage message = null;

            if (headers.Contains("content-length"))
            {
                message         = new ActiveMQBytesMessage();
                message.Content = content;
            }
            else
            {
                message = new ActiveMQTextMessage(encoding.GetString(content, 0, content.Length));
            }

            // TODO now lets set the various headers

            message.Type             = RemoveHeader(headers, "type");
            message.Destination      = StompHelper.ToDestination(RemoveHeader(headers, "destination"));
            message.ReplyTo          = StompHelper.ToDestination(RemoveHeader(headers, "reply-to"));
            message.TargetConsumerId = StompHelper.ToConsumerId(RemoveHeader(headers, "subscription"));
            message.CorrelationId    = RemoveHeader(headers, "correlation-id");
            message.MessageId        = StompHelper.ToMessageId(RemoveHeader(headers, "message-id"));
            message.Persistent       = StompHelper.ToBool(RemoveHeader(headers, "persistent"), true);

            string header = RemoveHeader(headers, "priority");

            if (header != null)
            {
                message.Priority = Byte.Parse(header);
            }

            header = RemoveHeader(headers, "timestamp");
            if (header != null)
            {
                message.Timestamp = Int64.Parse(header);
            }

            header = RemoveHeader(headers, "expires");
            if (header != null)
            {
                message.Expiration = Int64.Parse(header);
            }

            // now lets add the generic headers
            foreach (string key in headers.Keys)
            {
                Object value = headers[key];
                if (value != null)
                {
                    // lets coerce some standard header extensions
                    if (key == "NMSXGroupSeq")
                    {
                        value = Int32.Parse(value.ToString());
                    }
                }
                message.Properties[key] = value;
            }
            MessageDispatch dispatch = new MessageDispatch();

            dispatch.Message     = message;
            dispatch.ConsumerId  = message.TargetConsumerId;
            dispatch.Destination = message.Destination;
            return(dispatch);
        }
コード例 #23
0
        public void TestWriteOnlyBody()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            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)
            {
            }
        }
コード例 #24
0
        public void TestBytesMessageTransformation()
        {
            ActiveMQBytesMessage message = new ActiveMQBytesMessage();

            Assert.AreSame(message, transformer.TransformMessage <ActiveMQMessage>(message));
        }