protected void AssertMessageIsReadOnly(IMessage message) { IBytesMessage theMessage = message as IBytesMessage; Assert.IsNotNull(theMessage); try { theMessage.WriteBoolean(true); theMessage.WriteByte((byte)1); theMessage.WriteBytes(new byte[1]); theMessage.WriteBytes(new byte[3], 0, 2); theMessage.WriteChar('a'); theMessage.WriteDouble(1.5); theMessage.WriteSingle((float)1.5); theMessage.WriteInt32(1); theMessage.WriteInt64(1); theMessage.WriteObject("stringobj"); theMessage.WriteInt16((short)1); theMessage.WriteString("utfstring"); Assert.Fail("Message should not have been Writable"); } catch (MessageNotWriteableException) { } }
public override IBytesMessage CreateBytesMessage(byte[] body) { IBytesMessage msg = CreateBytesMessage(); msg.WriteBytes(body); return(msg); }
public void writeMessage(IBytesMessage msg, byte[] bytes) { msg.Properties.SetString("id", _id); msg.Properties.SetInt("index", _trivium.currentIndex()); _trivium.run(bytes); msg.WriteBytes(bytes); }
public void SendMessage(object obj) { if (obj == null) { return; } object sobj = _serialization.Serialize(obj); if (sobj is byte[]) { byte[] bytes = (byte[])sobj; if (bytes == null || bytes.Length == 0) { return; } IBytesMessage message = _service.session.CreateBytesMessage(); message.WriteBytes(bytes); _producer.Send(message); } else if (sobj is string) { string str = (string)sobj; ITextMessage txt = _service.session.CreateTextMessage(); _producer.Send(txt); } else { throw new Exception("obj is undefined type!"); } }
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); } }
public void TestTmpQueueWorksUnderLoad() { int count = 500; int dataSize = 1024; ArrayList list = new ArrayList(count); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.CreateTemporaryQueue(); IMessageProducer producer = session.CreateProducer(queue); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; byte[] data = new byte[dataSize]; for (int i = 0; i < count; i++) { IBytesMessage message = session.CreateBytesMessage(); message.WriteBytes(data); message.Properties.SetInt("c", i); producer.Send(message); list.Add(message); } connection.Start(); IMessageConsumer consumer = session.CreateConsumer(queue); for (int i = 0; i < count; i++) { IMessage message2 = consumer.Receive(TimeSpan.FromMilliseconds(2000)); Assert.IsTrue(message2 != null); Assert.AreEqual(i, message2.Properties.GetInt("c")); Assert.IsTrue(message2.Equals(list[i])); } }
public IBytesMessage CreateBytesMessage(byte[] body) { this.ThrowIfClosed(); IBytesMessage msg = CreateBytesMessage(); msg.WriteBytes(body); return(msg); }
protected void AssertMessageIsReadOnly(IMessage message) { Type writeableExceptionType = typeof(MessageNotWriteableException); IBytesMessage theMessage = message as IBytesMessage; Assert.IsNotNull(theMessage); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteBoolean(true); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteByte((byte)1); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteBytes(new byte[1]); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteBytes(new byte[3], 0, 2); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteChar('a'); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteDouble(1.5); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteSingle((float)1.5); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteInt32(1); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteInt64(1); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteObject("stringobj"); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteInt16((short)1); }); Assert.Throws(writeableExceptionType, delegate() { theMessage.WriteString("utfstring"); }); }
public void SendReceiveBytesMessageContent( [Values(MsgDeliveryMode.Persistent, MsgDeliveryMode.NonPersistent)] MsgDeliveryMode deliveryMode) { using (IConnection connection = CreateConnection(GetTestClientId())) { connection.Start(); using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { IDestination destination = CreateDestination(session, DestinationType.Queue); using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageProducer producer = session.CreateProducer(destination)) { producer.DeliveryMode = deliveryMode; IBytesMessage request = session.CreateBytesMessage(); request.WriteBoolean(true); request.WriteByte((byte)1); request.WriteBytes(new byte[1]); request.WriteBytes(new byte[3], 0, 2); request.WriteChar('a'); request.WriteDouble(1.5); request.WriteSingle((float)1.5); request.WriteInt32(1); request.WriteInt64(1); request.WriteObject("stringobj"); request.WriteInt16((short)1); request.WriteString("utfstring"); producer.Send(request); IMessage message = consumer.Receive(receiveTimeout); AssertMessageIsReadOnly(message); AssertBytesMessageEqual(request, message); Assert.AreEqual(deliveryMode, message.NMSDeliveryMode, "NMSDeliveryMode does not match"); } } } }
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); }
public void SendReceiveBytesMessageContentTest(MsgDeliveryMode deliveryMode) { using (IConnection connection = CreateConnection(TEST_CLIENT_ID + ":" + new Random().Next())) { connection.Start(); using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { IDestination destination = session.CreateTemporaryTopic(); using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageProducer producer = session.CreateProducer(destination)) { producer.DeliveryMode = deliveryMode; producer.RequestTimeout = receiveTimeout; IBytesMessage request = session.CreateBytesMessage(); request.WriteBoolean(true); request.WriteByte((byte)1); request.WriteBytes(new byte[1]); request.WriteBytes(new byte[3], 0, 2); request.WriteChar('a'); request.WriteDouble(1.5); request.WriteSingle((float)1.5); request.WriteInt32(1); request.WriteInt64(1); request.WriteObject("stringobj"); request.WriteInt16((short)1); request.WriteString("utfstring"); producer.Send(request); IMessage message = consumer.Receive(receiveTimeout); AssertBytesMessageEqual(request, message); AssertMessageIsReadOnly(message); Assert.AreEqual(deliveryMode, message.NMSDeliveryMode, "NMSDeliveryMode does not match"); } } } }
private IMessage createMessageOfSize(int size) { IBytesMessage message = channel[0].CreateBytesMessage(); string messageStr = "Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- "; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] messageBytes = encoding.GetBytes(messageStr); if (size > 0) { int div = size / messageBytes.Length; int mod = size % messageBytes.Length; for (int i = 0; i < div; i++) { message.WriteBytes(messageBytes); } if (mod != 0) { message.WriteBytes(messageBytes, 0, mod); } } return(message); }
/// <summary> /// 发送字节消息 /// </summary> /// <param name="message">字节消息</param> /// <returns></returns> public bool SendByteMessage(byte[] message) { bool bRet = false; try { IBytesMessage writer = this._session.CreateBytesMessage(); writer.WriteBytes(message); this._producer.Send(writer); bRet = true; } catch (Exception ex) { LogHelper.WriteErrorLog(string.Format("ActiveMQ发送字节消息异常:{0}", ex.Message)); } return(bRet); }
public static IBytesMessage packMessage(object t) { MemoryStream outs = new MemoryStream(); Encoder e = new BinaryEncoder(outs); var st = new PrimitiveSchema(, null); Encode <T> w = new Encode <T>((e0, t0) => {}); w(e, t); outs.Flush(); IBytesMessage message = null; byte[] data = outs.ToArray(); message.WriteBytes(data); return(message); }
/// <summary> /// 批量发送字节消息 /// </summary> /// <param name="msgList">字节消息列表</param> /// <returns></returns> public bool SendByteListMessage(List <byte[]> msgList) { bool bRet = false; try { IBytesMessage writer = this._session.CreateBytesMessage(); foreach (byte[] msg in msgList) { writer.WriteBytes(msg); this._producer.Send(writer); } bRet = true; } catch (Exception ex) { LogHelper.WriteErrorLog(string.Format("ActiveMQ批量发送字节消息异常:{0}", ex.Message)); } return(bRet); }
public static void Convert(TransportMessage message, IBytesMessage toSend, IMessageSerializer serializer) { byte[] body; if (message.Body == null && message.BodyStream != null) { body = message.BodyStream.ToBytes(); } else { var stream = new MemoryStream(); serializer.Serialize(message.Body, stream); body = stream.ToBytes(); } toSend.WriteBytes(body); // TODO: clarify usage of JMSCorrelationID toSend.JMSCorrelationID = message.CorrelationId; toSend.JMSDeliveryMode = message.Recoverable ? DeliveryMode.Persistent : DeliveryMode.NonPersistent; toSend.SetStringProperty(HEADER_RETURNADDRESS, message.ReturnAddress); toSend.SetStringProperty(HEADER_IDFORCORRELATION, message.IdForCorrelation); toSend.SetStringProperty(HEADER_WINDOWSIDENTITYNAME, message.WindowsIdentityName); toSend.SetIntProperty(HEADER_MESSAGEINTENT, (int) message.MessageIntent); //TODO: set message expiration //toSend.JMSReplyTo = new Destination message.ReplyToAddress; //if (message.TimeToBeReceived < MessageQueue.InfiniteTimeout) //toSend.JMSExpiration = (long) UTCNow.message.TimeToBeReceived.TotalMilliseconds; if (message.Headers == null) message.Headers = new List<HeaderInfo>(); var nsbHeaderKeys = new List<string>(); foreach (var keyValue in message.Headers) { toSend.SetStringProperty(keyValue.Key.ToXmsFriendly(), keyValue.Value); nsbHeaderKeys.Add(keyValue.Key.ToXmsFriendly()); } toSend.SetStringProperty(HEADER_NBSKEYS, WrapKeys(nsbHeaderKeys)); }
public static void Convert(TransportMessage message, IBytesMessage toSend) { if (message.Body != null) toSend.WriteBytes(message.Body); // TODO: clarify usage of JMSCorrelationID if (message.CorrelationId != null) toSend.JMSCorrelationID = message.CorrelationId; if (message.ReplyToAddress != null) toSend.SetStringProperty(HEADER_RETURNADDRESS, message.ReplyToAddress.ToString()); if (message.IdForCorrelation != null) toSend.SetStringProperty(HEADER_IDFORCORRELATION, message.IdForCorrelation ?? string.Empty); toSend.JMSDeliveryMode = message.Recoverable ? DeliveryMode.Persistent : DeliveryMode.NonPersistent; //toSend.SetStringProperty(HEADER_WINDOWSIDENTITYNAME, message.WindowsIdentityName); toSend.SetIntProperty(HEADER_MESSAGEINTENT, (int)message.MessageIntent); //TODO: set message expiration //toSend.JMSReplyTo = new Destination message.ReplyToAddress; //if (message.TimeToBeReceived < MessageQueue.InfiniteTimeout) //toSend.JMSExpiration = (long) UTCNow.message.TimeToBeReceived.TotalMilliseconds; //if (message.TimeToBeReceived < MessageQueue.InfiniteTimeout) //toSend.TimeToBeReceived = message.TimeToBeReceived; if (message.Headers == null) message.Headers = new Dictionary<string, string>(); if (!message.Headers.ContainsKey("CorrId")) message.Headers.Add("CorrId", null); if (string.IsNullOrEmpty(message.Headers["CorrId"])) message.Headers["CorrId"] = message.IdForCorrelation ?? string.Empty; var nsbHeaderKeys = new List<string>(); foreach (var keyValue in message.Headers) { toSend.SetStringProperty(keyValue.Key.ToXmsFriendly(), keyValue.Value); nsbHeaderKeys.Add(keyValue.Key.ToXmsFriendly()); } toSend.SetStringProperty(HEADER_NBSKEYS, WrapKeys(nsbHeaderKeys)); }
public void WriteBytes(byte[] value) { message.WriteBytes(value); }
/// <summary> /// 实现发送数据方法 /// </summary> public bool SendMsg(MQMsgType msgType, string topicName, byte[] data, IDictionary <string, object> properties, int timeToLiveMS, out string msgId) { try { msgId = ""; if (Connection == null) { this.logHelper.LogInfoMsg("MQProducer获取到的连接为空,可能是该MQ控件没有启动"); return(false); } if (!Connection.IsStarted) { this.logHelper.LogInfoMsg("MQProducer没有连接到MQ"); return(false); } //建立ISession,会话,一个发送或接收消息的线程 ISession session = Connection.CreateSession(); //创建Producer接受消息 if (msgType == MQMsgType.TOPIC) { ITopic topic = SessionUtil.GetTopic(session, topicName); Producer = session.CreateProducer(topic); } else if (msgType == MQMsgType.QUEUE) { IQueue queue = SessionUtil.GetQueue(session, topicName); Producer = session.CreateProducer(queue); } //持久化 Producer.DeliveryMode = MsgDeliveryMode.NonPersistent; //创建消息 IBytesMessage ibytesMessage = session.CreateBytesMessage(); if (properties != null) { //设置消息属性 foreach (KeyValuePair <string, object> pair in properties) { ibytesMessage.Properties[pair.Key] = pair.Value; } } if (data != null && data.Length > 0) { ibytesMessage.WriteBytes(data); } //发送超时时间,如果timeToLive == 0 则永远不过期 if (timeToLiveMS != 0) { Producer.TimeToLive = TimeSpan.FromMilliseconds((double)timeToLiveMS); } //向MQ发送消息 Producer.Send(ibytesMessage); msgId = ibytesMessage.NMSMessageId; return(true); } catch (Exception ex) { this.logHelper.LogErrMsg(ex, "MQProducer发送消息异常"); msgId = string.Empty; return(false); } }
// TODO: test this public static void PopulateErrorQueueMessage(IBytesMessage toSend, IBM.XMS.IBytesMessage failed, XmsDestination xmsDestination) { var body = new byte[failed.BodyLength]; if (body.Length > 0) { failed.ReadBytes(body, body.Length); toSend.WriteBytes(body); } toSend.JMSCorrelationID = failed.JMSCorrelationID; toSend.JMSDeliveryMode = failed.JMSDeliveryMode; toSend.CopyStringProperty(HEADER_RETURNADDRESS, failed); toSend.CopyStringProperty(HEADER_IDFORCORRELATION, failed); toSend.CopyStringProperty(HEADER_WINDOWSIDENTITYNAME, failed); toSend.CopyStringProperty(HEADER_MESSAGEINTENT, failed); var keys = failed.GetStringProperty(HEADER_NBSKEYS); var unwrapedKeys = UnwrapKeys(keys); foreach (var unwrapedKey in unwrapedKeys) { toSend.CopyStringProperty(unwrapedKey, failed); } toSend.CopyStringProperty(HEADER_NBSKEYS, failed); // error queue specific toSend.SetStringProperty(HEADER_FAILEDQUEUE, xmsDestination.ToString()); var id = failed.GetStringProperty(HEADER_ORIGINALID); toSend.SetStringProperty(HEADER_ORIGINALID, id); }