Esempio n. 1
0
        public void Send(Endpoint destination, object[] msgs)
        {
            if (HaveStarted == false)
            {
                throw new InvalidOperationException("Cannot send a message before transport is started");
            }

            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages    = msgs,
                Source      = Endpoint
            };
            var message = GenerateMsmqMessageFromMessageBatch(messageInformation);

            SendMessageToQueue(message, destination);

            var copy = MessageSent;

            if (copy == null)
            {
                return;
            }

            copy(new CurrentMessageInformation
            {
                AllMessages = msgs,
                Source      = Endpoint.Uri,
                Destination = destination.Uri,
                MessageId   = message.GetMessageId(),
            });
        }
Esempio n. 2
0
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     if (messageInformation.Messages[0] is ICustomizeMessageByType)
     {
         messageInformation.MaxAttempts = 1;
     }
 }
Esempio n. 3
0
        public void Send(Endpoint endpoint, DateTime processAgainAt, object[] msgs)
        {
            if (HaveStarted == false)
            {
                throw new InvalidOperationException("Cannot send a message before transport is started");
            }

            var messageInformation = new OutgoingMessageInformation
            {
                Destination = endpoint,
                Messages    = msgs,
                Source      = Endpoint
            };
            var message           = GenerateMsmqMessageFromMessageBatch(messageInformation);
            var processAgainBytes = BitConverter.GetBytes(processAgainAt.ToBinary());

            if (message.Extension.Length == 16)
            {
                var bytes = new List <byte>(message.Extension);
                bytes.AddRange(processAgainBytes);
                message.Extension = bytes.ToArray();
            }
            else
            {
                var extension = (byte[])message.Extension.Clone();
                Buffer.BlockCopy(processAgainBytes, 0, extension, 16, processAgainBytes.Length);
                message.Extension = extension;
            }
            message.AppSpecific = (int)MessageType.TimeoutMessageMarker;

            SendMessageToQueue(message, endpoint);
        }
Esempio n. 4
0
        public void CanCustomizeMessageBasedOnDestination()
        {
            using (var container = new WindsorContainer())
            {
                container.Register(Component.For <ICustomizeOutgoingMessages>().ImplementedBy <CustomizeByDestination>());
                new RhinoServiceBusConfiguration()
                .UseCastleWindsor(container)
                .UseStandaloneConfigurationFile("RhinoQueues/RhinoQueues.config")
                .Configure();

                var builder = container.Resolve <IMessageBuilder <MessagePayload> >();
                builder.Initialize(new Endpoint {
                    Uri = RhinoQueuesOneWayBus.NullEndpoint
                });
                var messageInfo = new OutgoingMessageInformation
                {
                    Destination = new Endpoint {
                        Uri = new Uri("null://nowhere/queue?Volatile=true")
                    },
                    Messages = new[] { "somemsg" }
                };
                var msg = builder.BuildFromMessageBatch(messageInfo);
                Assert.NotNull(msg);
                Assert.NotEqual(0, msg.Data.Length);
                Assert.Equal(2, msg.MaxAttempts);
            }
        }
        private void SendInternal(object[] msgs, Endpoint destination, Action <NameValueCollection> customizeHeaders)
        {
            var messageId          = Guid.NewGuid();
            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages    = msgs,
                Source      = Endpoint
            };
            var payload = messageBuilder.BuildFromMessageBatch(messageInformation);

            logger.DebugFormat("Sending a message with id '{0}' to '{1}'", messageId, destination.Uri);
            customizeHeaders(payload.Headers);

            _sqlQueueManager.Send(destination.Uri, payload);

            var copy = MessageSent;

            if (copy == null)
            {
                return;
            }

            copy(new SqlQueueCurrentMessageInformation
            {
                AllMessages = msgs,
                Source      = Endpoint.Uri,
                Destination = destination.Uri,
                MessageId   = messageId,
            });
        }
Esempio n. 6
0
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     if (messageInformation.Destination != null &&
         messageInformation.Destination.Uri.Query.Contains("Volatile"))
     {
         messageInformation.MaxAttempts = 2;
     }
 }
Esempio n. 7
0
        protected Message GenerateMsmqMessageFromMessageBatch(params object[] msgs)
        {
            var messageInformation = new OutgoingMessageInformation
            {
                Messages = msgs,
                Source   = Endpoint
            };

            return(GenerateMsmqMessageFromMessageBatch(messageInformation));
        }
Esempio n. 8
0
            public MessagePayload BuildFromMessageBatch(OutgoingMessageInformation messageInformation)
            {
                var payload = inner.BuildFromMessageBatch(messageInformation);

                Contextualize(payload);

                if (MessageBuilt != null)
                {
                    MessageBuilt(payload);
                }
                return(payload);
            }
Esempio n. 9
0
 public void Send(params object[] msgs)
 {
     var endpoint = messageOwners.GetEndpointForMessageBatch(msgs);
     var messageInformation = new OutgoingMessageInformation
     {
         Destination = endpoint,
         Messages = msgs,
     };
     using(var queue = endpoint.InitalizeQueue())
     {
         var message = messageBuilder.BuildFromMessageBatch(messageInformation);
         queue.SendInSingleTransaction(message);
     }
 }
Esempio n. 10
0
        public void Send(params object[] msgs)
        {
            var endpoint           = messageOwners.GetEndpointForMessageBatch(msgs);
            var messageInformation = new OutgoingMessageInformation
            {
                Destination = endpoint,
                Messages    = msgs,
            };

            using (var queue = endpoint.InitalizeQueue())
            {
                var message = messageBuilder.BuildFromMessageBatch(messageInformation);
                queue.SendInSingleTransaction(message);
            }
        }
        public MessagePayload BuildFromMessageBatch(OutgoingMessageInformation messageInformation)
        {
            if (endpoint == null)
            {
                throw new InvalidOperationException("A source endpoint is required for Rhino Queues transport, did you Initialize me? try providing a null Uri.");
            }

            var messageId = Guid.NewGuid();

            byte[] data = new byte[0];
            using (var memoryStream = new MemoryStream())
            {
                messageSerializer.Serialize(messageInformation.Messages, memoryStream);
                data = memoryStream.ToArray();
            }
            var payload = new MessagePayload
            {
                Data    = data,
                Headers =
                {
                    { "id",     messageId.ToString()                                         },
                    { "type",   GetAppSpecificMarker(messageInformation.Messages).ToString() },
                    { "source", endpoint.Uri.ToString()                                      },
                }
            };

            messageInformation.Headers = payload.Headers;
            foreach (var customizeHeader in customizeHeaders)
            {
                customizeHeader.Customize(messageInformation);
            }

            payload.DeliverBy   = messageInformation.DeliverBy;
            payload.MaxAttempts = messageInformation.MaxAttempts;

            var copy = MessageBuilt;

            if (copy != null)
            {
                copy(payload);
            }

            return(payload);
        }
Esempio n. 12
0
        public void it_should_add_custom_header_to_headers_collection_using_interface()
        {
            using (var container = new WindsorContainer())
            {
                container.Register(Component.For <ICustomizeOutgoingMessages>().ImplementedBy <AppIdentityCustomizer>().LifeStyle.Is(LifestyleType.Transient));
                new RhinoServiceBusConfiguration()
                .UseCastleWindsor(container)
                .UseStandaloneConfigurationFile("RhinoQueues/RhinoQueues.config")
                .Configure();

                var builder = container.Resolve <IMessageBuilder <MessagePayload> >();
                builder.Initialize(new Endpoint {
                    Uri = RhinoQueuesOneWayBus.NullEndpoint
                });
                var messageInfo = new OutgoingMessageInformation {
                    Messages = new[] { "somemsg" }
                };
                var msg = builder.BuildFromMessageBatch(messageInfo);
                Assert.NotNull(msg);
                Assert.NotEqual(0, msg.Data.Length);
                Assert.Equal("mikey", msg.Headers["user-id"]);
            }
        }
Esempio n. 13
0
        private void SendInternal(object[] msgs, Endpoint destination, Action <NameValueCollection> customizeHeaders)
        {
            var messageId          = Guid.NewGuid();
            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages    = msgs,
                Source      = Endpoint
            };
            var payload = messageBuilder.BuildFromMessageBatch(messageInformation);

            logger.DebugFormat("Sending a message with id '{0}' to '{1}'", messageId, destination.Uri);
            customizeHeaders(payload.Headers);
            var transactionOptions = GetTransactionOptions();

            using (var tx = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                queueManager.Send(destination.Uri, payload);
                tx.Complete();
            }

            var copy = MessageSent;

            if (copy == null)
            {
                return;
            }

            copy(new RhinoQueueCurrentMessageInformation
            {
                AllMessages = msgs,
                Source      = Endpoint.Uri,
                Destination = destination.Uri,
                MessageId   = messageId,
            });
        }
Esempio n. 14
0
 protected Message GenerateMsmqMessageFromMessageBatch(OutgoingMessageInformation messageInformation)
 {
     return(messageBuilder.BuildFromMessageBatch(messageInformation));
 }
Esempio n. 15
0
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.Headers.Add("user-id", "mikey");
 }
Esempio n. 16
0
	    public void Send(Endpoint endpoint, DateTime processAgainAt, object[] msgs)
		{
			if (HaveStarted == false)
				throw new InvalidOperationException("Cannot send a message before transport is started");

            var messageInformation = new OutgoingMessageInformation
            {
                Destination = endpoint,
                Messages = msgs,
                Source = Endpoint
            };
			var message = GenerateMsmqMessageFromMessageBatch(messageInformation);
	        var processAgainBytes = BitConverter.GetBytes(processAgainAt.ToBinary());
            if (message.Extension.Length == 16)
            {
                var bytes = new List<byte>(message.Extension);
                bytes.AddRange(processAgainBytes);
                message.Extension = bytes.ToArray();
            }
            else
            {
                var extension = (byte[])message.Extension.Clone();
                Buffer.BlockCopy(processAgainBytes, 0, extension, 16, processAgainBytes.Length);
                message.Extension = extension;
            }
	        message.AppSpecific = (int)MessageType.TimeoutMessageMarker;

            SendMessageToQueue(message, endpoint);
		}
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.DeliverBy = DateTime.Now.AddMinutes(1);
 }
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.MaxAttempts = MaxAttempts;
 }
Esempio n. 19
0
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.MaxAttempts = MaxAttempts;
 }
        private void SendInternal(object[] msgs, Endpoint destination, Action<NameValueCollection> customizeHeaders)
        {
            var messageId = Guid.NewGuid();
            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages = msgs,
                Source = Endpoint
            };
            var payload = messageBuilder.BuildFromMessageBatch(messageInformation);
            logger.DebugFormat("Sending a message with id '{0}' to '{1}'", messageId, destination.Uri);
            customizeHeaders(payload.Headers);

            _sqlQueueManager.Send(destination.Uri, payload);

            var copy = MessageSent;
            if (copy == null)
                return;

            copy(new SqlQueueCurrentMessageInformation
            {
                AllMessages = msgs,
                Source = Endpoint.Uri,
                Destination = destination.Uri,
                MessageId = messageId,
            });
        }
Esempio n. 21
0
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.DeliverBy = DateTime.Now.AddMinutes(1);
 }
 public void Customize(OutgoingMessageInformation messageInformation)
 {
     messageInformation.Headers.Add("user-id", "corey");
 }
Esempio n. 23
0
 protected Message GenerateMsmqMessageFromMessageBatch(params object[] msgs)
 {
     var messageInformation = new OutgoingMessageInformation
     {
         Messages = msgs,
         Source = Endpoint
     };
     return GenerateMsmqMessageFromMessageBatch(messageInformation);
 }
Esempio n. 24
0
 protected Message GenerateMsmqMessageFromMessageBatch(OutgoingMessageInformation messageInformation)
 {
     return messageBuilder.BuildFromMessageBatch(messageInformation);
 }
Esempio n. 25
0
        public void Send(Endpoint destination, object[] msgs)
		{
			if(HaveStarted==false)
				throw new InvalidOperationException("Cannot send a message before transport is started");

            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages = msgs,
                Source = Endpoint
            };
            var message = GenerateMsmqMessageFromMessageBatch(messageInformation);

            SendMessageToQueue(message, destination);

			var copy = MessageSent;
			if (copy == null)
				return;

			copy(new CurrentMessageInformation
			{
				AllMessages = msgs,
				Source = Endpoint.Uri,
				Destination = destination.Uri,
                MessageId = message.GetMessageId(),
			});
		}
        private void SendInternal(object[] msgs, Endpoint destination, Action<NameValueCollection> customizeHeaders)
        {
            var messageId = Guid.NewGuid();
            var messageInformation = new OutgoingMessageInformation
            {
                Destination = destination,
                Messages = msgs,
                Source = Endpoint
            };
            var payload = messageBuilder.BuildFromMessageBatch(messageInformation);
            logger.DebugFormat("Sending a message with id '{0}' to '{1}'", messageId, destination.Uri);
            customizeHeaders(payload.Headers);
            var transactionOptions = GetTransactionOptions();
            using (var tx = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                queueManager.Send(destination.Uri, payload);
                tx.Complete();
            }

            var copy = MessageSent;
            if (copy == null)
                return;

            copy(new RhinoQueueCurrentMessageInformation
            {
                AllMessages = msgs,
                Source = Endpoint.Uri,
                Destination = destination.Uri,
                MessageId = messageId,
            });
        }
Esempio n. 27
0
        public Message BuildFromMessageBatch(OutgoingMessageInformation messageInformation)
        {
            var msgs    = messageInformation.Messages;
            var message = new Message();

            var isAdmin = msgs.Any(x => x is AdministrativeMessage);

            try
            {
                messageSerializer.Serialize(msgs, message.BodyStream);
            }
            catch (SerializationException ex)
            {
                logger.Error("Error when trying to serialize message.", ex);
                throw;
            }
            message.Priority = isAdmin ? MessagePriority.High : MessagePriority.Normal;
            if (endpoint != null)
            {
                message.ResponseQueue = endpoint.InitalizeQueue().ToResponseQueue();
            }
            else
            {
                message.ResponseQueue = null;
            }

            byte[] extension;
            var    messageId = Guid.NewGuid().ToByteArray();

            if (customizeHeaders.Length > 0)
            {
                messageInformation.Headers = new NameValueCollection();
                foreach (var customizeHeader in customizeHeaders)
                {
                    customizeHeader.Customize(messageInformation);
                }
                var headerBytes = messageInformation.Headers.SerializeHeaders();
                //accounts for existing use of Extension for messageId and deferred messages
                extension = new byte[24 + headerBytes.Length];
                Buffer.BlockCopy(messageId, 0, extension, 0, messageId.Length);
                Buffer.BlockCopy(headerBytes, 0, extension, 24, headerBytes.Length);
            }
            else
            {
                extension = messageId;
            }

            if (messageInformation.DeliverBy != null)
            {
                var timeFromNow = messageInformation.DeliverBy.Value - DateTime.Now;
                message.TimeToReachQueue = timeFromNow > TimeSpan.Zero ? timeFromNow : TimeSpan.Zero;
            }

            if (messageInformation.MaxAttempts != null)
            {
                if (messageInformation.MaxAttempts != 1)
                {
                    throw new InvalidUsageException("MSMQ does not support a maximum number of attempts other than 1");
                }

                message.TimeToReachQueue = TimeSpan.Zero;
            }

            message.Extension = extension;

            message.AppSpecific = GetAppSpecificMarker(msgs);

            message.Label = msgs
                            .Where(msg => msg != null)
                            .Select(msg =>
            {
                string s = msg.ToString();
                if (s.Length > 249)
                {
                    return(s.Substring(0, 246) + "...");
                }
                return(s);
            })
                            .FirstOrDefault();

            var copy = MessageBuilt;

            if (copy != null)
            {
                copy(message);
            }

            return(message);
        }