Exemple #1
0
        public void SendMqMessage <TPropery>(MessageType messageType, string strText, List <TPropery> lstProperty) where TPropery : Property
        {
            try
            {
                IMessage msg;
                if (messageType.Equals(MessageType.Text))
                {
                    msg = producer.CreateTextMessage();
                    ((ITextMessage)msg).Text = strText;
                }
                else
                {
                    msg = producer.CreateBytesMessage();
                    ((IBytesMessage)msg).Content = Encoding.Default.GetBytes(strText);
                }

                foreach (Property prop in lstProperty)
                {
                    msg.Properties.SetString(prop.name, prop.value);
                }
                producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
            }
            catch (System.Exception ex)
            {
            }
        }
Exemple #2
0
        public void SendMQMessage(bool blnText, string strText, List <Property> lstProperty)
        {
            try
            {
                IMessage msg;
                if (blnText)
                {
                    msg = producer.CreateTextMessage();
                    ((ITextMessage)msg).Text = strText;
                }
                else
                {
                    msg = producer.CreateBytesMessage();
                    ((IBytesMessage)msg).Content = Encoding.Default.GetBytes(strText);
                }

                foreach (Property prop in lstProperty)
                {
                    msg.Properties.SetString(prop.name, prop.value);
                }
                producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
            }
            catch (System.Exception ex)
            {
                GlobalFunction.MsgBoxException(ex.Message, "SendMQMessage");
            }
        }
Exemple #3
0
        public bool SendMQMessage(bool blnText, string strText, List <Property> lstProperty)
        {
            bool sended = false;

            try
            {
                IMessage msg;
                if (blnText)
                {
                    msg = producer.CreateTextMessage();
                    ((ITextMessage)msg).Text = strText;
                }
                else
                {
                    msg = producer.CreateBytesMessage();
                    ((IBytesMessage)msg).Content = Encoding.Default.GetBytes(strText);
                }

                foreach (Property prop in lstProperty)
                {
                    msg.Properties.SetString(prop.name, prop.value);
                }
                producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
                sended = true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message, "SendMQMessage");
            }
            return(sended);
        }
Exemple #4
0
        protected void sendTags()
        {
            try
            {
                IBytesMessage msg = _producer.CreateBytesMessage();
                //msg.Properties.SetBytes("aa", new byte[5]);
                MemoryStream stream     = new MemoryStream();
                GZipStream   gZipStream = new GZipStream(stream, CompressionMode.Compress);
                byte[]       bytes      = _tagsBuilder.Build().ToByteArray();
                gZipStream.Write(bytes, 0, bytes.Length);
                gZipStream.Close();
                byte[] compressedBytes = stream.ToArray();
                if (_shouldCrypto)
                {
                    _keyAgreement.writeMessage(msg, compressedBytes);
                }
                else
                {
                    msg.WriteBytes(compressedBytes);
                }

                this._producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
                _lastDateTime = DateTime.Now;
                _tagsBuilder  = new XtiveTags.Builder();
            }

            catch (System.Exception e)
            {
                log.Error(e.Message);
            }
        }
Exemple #5
0
 private void SendMessage(byte[] messageData, string destinationName)
 {
     using (ISession session = mConnection.CreateSession())
         using (IDestination destination = session.GetTopic(destinationName))
             using (IMessageProducer producer = session.CreateProducer(destination))
             {
                 producer.Send(producer.CreateBytesMessage(messageData));
             }
 }
Exemple #6
0
        private void SendMessage()
        {
            //创建临时目的地,它们的存在时间只限于创建它们的连接所保持的时间
            //只有创建该临时目的地的连接上的消息消费者才能够从临时目的地中提取消息
            //ITemporaryQueue queue = m_session.CreateTemporaryQueue();

            IQueue queue = m_session.GetQueue("test_Queue");

            m_producer = m_session.CreateProducer(queue);



            //TextMessage消息类型,  普通文本消息
            ITextMessage textMessage = m_producer.CreateTextMessage();

            textMessage.Text = this.txtBox_消息.Text;

            //MapMessafe消息类型,   Map集合消息
            IMapMessage mapMessage = m_producer.CreateMapMessage();

            m_producer.Send(mapMessage);

            //ObjectMessage消息类型, 发送序列化对象
            Queue <string> obj = new Queue <string>();

            obj.Enqueue("one");
            obj.Enqueue("two");
            IObjectMessage objectMessage = m_producer.CreateObjectMessage(obj);

            m_producer.Send(objectMessage);

            //BytesMessage消息类型, 发送字节消息
            IBytesMessage bytesMessage = m_producer.CreateBytesMessage();

            bytesMessage.WriteBytes(System.Text.Encoding.Default.GetBytes("字节"));
            m_producer.Send(bytesMessage);

            //其它消息类型大同小异

            //设置是否持久化
            //m_producer.DeliveryMode= MsgDeliveryMode.Persistent;

            //设置优先级 默认 MsgPriority.BelowNormal 4级
            //m_producer.Priority = MsgPriority.BelowNormal;

            //设置过期时间,5秒后 只有Queue点对点有效
            //m_producer.TimeToLive=new TimeSpan(1000*5);

            //发送超时时间,默认等于0,如果设置超时,则所有的消息都是用同步发送
            //m_producer.RequestTimeout = new TimeSpan(1000*20);
        }
Exemple #7
0
        protected virtual IMessage GenerateMessage(TMessage message, IMessageProducer producer, IMessageSerializer serializer)
        {
            var bytesMessage = producer.CreateBytesMessage();

            using (var stream = StreamManager.Instance.GetStream())
            {
                var offset = stream.Position;

                serializer.Serialize(stream, message);

                bytesMessage.WriteBytes(stream.GetBuffer(), (int)offset, (int)(stream.Position - offset));
            }

            return(bytesMessage);
        }
Exemple #8
0
        public async Task ShouldSendBytesMessageSerializerReturnsBytes()
        {
            _publisher = new MessagePublisher <IMessage>(_lazyConnection, _destination, _serializer, _propertyProvider, _testScheduler);

            ITestMessage testMessage = new TestMessage();

            byte[]        bytes        = new byte[0];
            IBytesMessage bytesMessage = A.Fake <IBytesMessage>();

            A.CallTo(() => _serializer.Serialize(A <Stream> ._, testMessage)).Invokes(arg => arg.Arguments.Get <Stream>(0).Write(bytes, 0, bytes.Length));
            A.CallTo(() => _producer.CreateBytesMessage()).Returns(bytesMessage);

            await _publisher.PublishAsync(testMessage);

            A.CallTo(() => _producer.Send(bytesMessage, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.Zero)).MustHaveHappened(1, Times.Exactly);
        }
Exemple #9
0
        public TestMessagePublisher()
        {
            A.Fake <IConnectionFactory>();
            _connection     = A.Fake <IConnection>();
            _lazyConnection = new Lazy <IConnection>(() =>
            {
                _connection.Start();
                return(_connection);
            });
            _session     = A.Fake <ISession>();
            _producer    = A.Fake <IMessageProducer>();
            _serializer  = A.Fake <IMessageSerializer>();
            _destination = A.Fake <IDestination>();
            _message     = A.Fake <IBytesMessage>();

            A.CallTo(() => _connection.CreateSession(A <Apache.NMS.AcknowledgementMode> .Ignored)).Returns(_session);
            A.CallTo(() => _session.CreateProducer(_destination)).Returns(_producer);
            A.CallTo(() => _producer.CreateBytesMessage()).Returns(_message);
        }
Exemple #10
0
        /// <summary>
        /// 插入MQ
        /// </summary>
        /// <param name="buffer">字节流</param>
        public static void InsertMQ(byte[] buffer)
        {
            using (IConnection connection = _factory.CreateConnection())
            {
                //Create the Session
                using (ISession session = connection.CreateSession())
                {
                    //Create the Producer for the topic/queue
                    IMessageProducer prod = session.CreateProducer(
                        new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(GlobalStatus.MQTopic));

                    var msg = prod.CreateBytesMessage();
                    msg.Content = buffer;
                    LogWriter.Info("SendingMQ: " + BitConverter.ToString(buffer));
                    prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent,
                              Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);
                }
            }
        }
        public void SetUp()
        {
            A.Fake<IConnectionFactory>();
            _connection = A.Fake<IConnection>();
            _lazyConnection = new Lazy<IConnection>(() =>
            {
                _connection.Start();
                return _connection;
            });
            _session = A.Fake<ISession>();
            _producer = A.Fake<IMessageProducer>();
            _serializer = A.Fake<IMessageSerializer>();
            _destination = A.Fake<IDestination>();
            _message = A.Fake<IBytesMessage>();
            _messagePropertyProvider = A.Fake<IMessagePropertyProvider<IMessage>>();

            A.CallTo(() => _connection.CreateSession(A<Apache.NMS.AcknowledgementMode>.Ignored)).Returns(_session);
            A.CallTo(() => _session.CreateProducer(_destination)).Returns(_producer);
            A.CallTo(() => _producer.CreateBytesMessage()).Returns(_message);

            _publisher = new MessagePublisher<IMessage>(_lazyConnection, _destination, _serializer, _messagePropertyProvider, _testScheduler);
        }
Exemple #12
0
 public IBytesMessage CreateBytesMessage()
 {
     Used?.Invoke();
     return(_producer.CreateBytesMessage());
 }
 /// <summary>
 /// Creates the bytes message.
 /// </summary>
 /// <returns>A new bytes message.</returns>
 public IBytesMessage CreateBytesMessage()
 {
     return(target.CreateBytesMessage());
 }
Exemple #14
0
        public void Send(byte[] message)
        {
            var bytesMessage = sender.CreateBytesMessage(message);

            sender.Send(bytesMessage);
        }