public virtual IMessage ToNmsMessage(Message message) { BaseMessage answer = CreateNmsMessage(message); // Get the NMS meta data from the extension area PrimitiveMap metaData = PrimitiveMap.Unmarshal(message.Extension); try { answer.NMSMessageId = message.Id; answer.NMSCorrelationID = metaData.GetString("NMSCorrelationID"); answer.NMSDeliveryMode = (message.Recoverable ? MsgDeliveryMode.Persistent : MsgDeliveryMode.NonPersistent); answer.NMSDestination = ToNmsDestination(message.DestinationQueue); } catch (InvalidOperationException) { } try { answer.NMSType = message.Label; answer.NMSReplyTo = ToNmsDestination(message.ResponseQueue); answer.NMSTimeToLive = message.TimeToBeReceived; } catch (InvalidOperationException) { } return(answer); }
public virtual Message ToMsmqMessage(IMessage message) { Message msmqMessage = new Message(); PrimitiveMap metaData = new PrimitiveMap(); ConvertMessageBodyToMSMQ(message, msmqMessage); if (message.NMSTimeToLive != TimeSpan.Zero) { msmqMessage.TimeToBeReceived = message.NMSTimeToLive; } if (message.NMSCorrelationID != null) { metaData.SetString("NMSCorrelationID", message.NMSCorrelationID); } msmqMessage.Recoverable = (message.NMSDeliveryMode == MsgDeliveryMode.Persistent); msmqMessage.Priority = ToMessagePriority(message.NMSPriority); msmqMessage.ResponseQueue = ToMsmqDestination(message.NMSReplyTo); if (message.NMSType != null) { msmqMessage.Label = message.NMSType; } // Store the NMS meta data in the extension area msmqMessage.Extension = metaData.Marshal(); return(msmqMessage); }
/// <summary> /// Converts an MSMQ message body to the equivalent NMS map message /// body. /// </summary> /// <param name="message">Source MSMQ message.</param> /// <param name="answer">Target NMS map message.</param> public virtual void ConvertMapMessageBodyToNMS(Message message, MapMessage answer) { byte[] buf = null; if (message.BodyStream != null && message.BodyStream.Length > 0) { buf = new byte[message.BodyStream.Length]; message.BodyStream.Read(buf, 0, buf.Length); } answer.Body = PrimitiveMap.Unmarshal(buf); }
/// <summary> /// Converts the specified NMS message to an equivalent MSMQ message. /// </summary> /// <param name="message">NMS message to be converted.</param> /// <result>Converted MSMQ message.</result> public virtual Message ToMsmqMessage(IMessage message) { Message msmqMessage = new Message(); PrimitiveMap propertyData = new PrimitiveMap(); ConvertMessageBodyToMSMQ(message, msmqMessage); if (message.NMSTimeToLive != TimeSpan.Zero) { msmqMessage.TimeToBeReceived = message.NMSTimeToLive; } if (message.NMSCorrelationID != null) { propertyData.SetString("NMSCorrelationID", message.NMSCorrelationID); } msmqMessage.Recoverable = (message.NMSDeliveryMode == MsgDeliveryMode.Persistent); msmqMessage.Priority = ToMsmqMessagePriority(message.NMSPriority); msmqMessage.ResponseQueue = ToMsmqDestination(message.NMSReplyTo); if (message.NMSType != null) { if (SetLabelAsNMSType) { propertyData.SetString("NMSType", message.NMSType); } else { msmqMessage.Label = message.NMSType; } } // Populate property data foreach (object keyObject in message.Properties.Keys) { string key = (keyObject as string); object val = message.Properties[key]; if (!SetLabelAsNMSType && string.Compare(key, "Label", true) == 0 && val != null) { msmqMessage.Label = val.ToString(); } else { propertyData[key] = val; } } // Store the NMS property data in the extension area msmqMessage.Extension = propertyData.Marshal(); return(msmqMessage); }
/// <summary> /// Converts an NMS message body to the equivalent MSMQ message body. /// </summary> /// <param name="message">Source NMS message.</param> /// <param name="answer">Target MSMQ message.</param> protected virtual void ConvertMessageBodyToMSMQ(IMessage message, Message answer) { if (message is TextMessage) { TextMessage textMessage = message as TextMessage; byte[] buf = Encoding.UTF32.GetBytes(textMessage.Text); answer.BodyStream.Write(buf, 0, buf.Length); answer.AppSpecific = (int)NMSMessageType.TextMessage; } else if (message is BytesMessage) { BytesMessage bytesMessage = message as BytesMessage; bytesMessage.Reset(); answer.BodyStream.Write(bytesMessage.Content, 0, bytesMessage.Content.Length); answer.AppSpecific = (int)NMSMessageType.BytesMessage; } else if (message is ObjectMessage) { ObjectMessage objectMessage = message as ObjectMessage; answer.Body = objectMessage.Body; answer.AppSpecific = (int)NMSMessageType.ObjectMessage; } else if (message is MapMessage) { MapMessage mapMessage = message as MapMessage; PrimitiveMap mapBody = mapMessage.Body as PrimitiveMap; byte[] buf = mapBody.Marshal(); answer.BodyStream.Write(buf, 0, buf.Length); answer.AppSpecific = (int)NMSMessageType.MapMessage; } else if (message is StreamMessage) { StreamMessage streamMessage = message as StreamMessage; answer.AppSpecific = (int)NMSMessageType.StreamMessage; // TODO: Implement } else if (message is BaseMessage) { answer.AppSpecific = (int)NMSMessageType.BaseMessage; } else { throw new Exception("unhandled message type"); } }
/// <summary> /// Converts the specified MSMQ message to an equivalent NMS message. /// </summary> /// <param name="message">MSMQ message to be converted.</param> /// <param name="convertBody">true if message body should be converted.</param> /// <result>Converted NMS message.</result> public virtual IMessage ToNmsMessage(Message message, bool convertBody) { BaseMessage answer = CreateNmsMessage(message, convertBody); // Get the NMS property data from the extension area PrimitiveMap propertyData = PrimitiveMap.Unmarshal(message.Extension); try { answer.NMSMessageId = message.Id; answer.NMSCorrelationID = propertyData.GetString("NMSCorrelationID"); answer.NMSDeliveryMode = (message.Recoverable ? MsgDeliveryMode.Persistent : MsgDeliveryMode.NonPersistent); answer.NMSDestination = ToNmsDestination(message.DestinationQueue); } catch (InvalidOperationException) { } try { answer.NMSReplyTo = ToNmsDestination(message.ResponseQueue); answer.NMSTimeToLive = message.TimeToBeReceived; answer.NMSPriority = ToNmsMsgPriority(message.Priority); } catch (InvalidOperationException) { } try { if (message.Label != null) { if (SetLabelAsNMSType) { answer.NMSType = message.Label; } else { answer.Properties["Label"] = message.Label; } } answer.Properties["LookupId"] = message.LookupId; } catch (InvalidOperationException) { } foreach (object keyObject in propertyData.Keys) { try { string key = (keyObject as string); if (string.Compare(key, "NMSType", true) == 0) { answer.NMSType = propertyData.GetString(key); } else if (string.Compare(key, "NMSCorrelationID", true) == 0) { answer.NMSCorrelationID = propertyData.GetString("NMSCorrelationID"); } else { answer.Properties[key] = propertyData[key]; } } catch (InvalidOperationException) { } } return(answer); }
protected virtual BaseMessage CreateNmsMessage(Message message) { BaseMessage result = null; if ((int)NMSMessageType.TextMessage == message.AppSpecific) { TextMessage textMessage = new TextMessage(); string content = String.Empty; if (message.BodyStream != null && message.BodyStream.Length > 0) { byte[] buf = null; buf = new byte[message.BodyStream.Length]; message.BodyStream.Read(buf, 0, buf.Length); content = Encoding.UTF32.GetString(buf); } textMessage.Text = content; result = textMessage; } else if ((int)NMSMessageType.BytesMessage == message.AppSpecific) { byte[] buf = null; if (message.BodyStream != null && message.BodyStream.Length > 0) { buf = new byte[message.BodyStream.Length]; message.BodyStream.Read(buf, 0, buf.Length); } BytesMessage bytesMessage = new BytesMessage(); bytesMessage.Content = buf; result = bytesMessage; } else if ((int)NMSMessageType.ObjectMessage == message.AppSpecific) { ObjectMessage objectMessage = new ObjectMessage(); objectMessage.Body = message.Body; result = objectMessage; } else if ((int)NMSMessageType.MapMessage == message.AppSpecific) { byte[] buf = null; if (message.BodyStream != null && message.BodyStream.Length > 0) { buf = new byte[message.BodyStream.Length]; message.BodyStream.Read(buf, 0, buf.Length); } MapMessage mapMessage = new MapMessage(); mapMessage.Body = PrimitiveMap.Unmarshal(buf); result = mapMessage; } else if ((int)NMSMessageType.StreamMessage == message.AppSpecific) { StreamMessage streamMessage = new StreamMessage(); // TODO: Implement result = streamMessage; } else { BaseMessage baseMessage = new BaseMessage(); result = baseMessage; } return(result); }