/// <summary>
        /// Handles a ScheduleMe that has expired from the Pending queue. If it is time to send the message, the message is published to the destination queue.
        /// If the time has not yet come, put the message back into the Pending queue with a new expiry time.
        /// </summary>
        /// <param name="message"></param>
        private void Evaluate(ScheduleMe message)
        {
            log.DebugWrite("Received message for publish at {0}", message.WakeTime);

            if (!bus.IsConnected)
            {
                return;
            }

            try
            {
                // If wake time is past, publish the message
                var timeToPublish = TimeToPublish(message);
                if (timeToPublish < 5000)
                {
                    Publish(message);
                }
                else
                {
                    // otherwise put it back on the pending queue
                    ReQueue(message, timeToPublish);
                }
            }
            catch (Exception ex)
            {
                log.ErrorWrite(ex);
            }
        }
Ejemplo n.º 2
0
        public void FuturePublish <T>(DateTime futurePublishDate, T message, string cancellationKey = null) where T : class
        {
            Preconditions.CheckNotNull(message, "message");
            var scheduleMeType      = typeof(ScheduleMe);
            var scheduleMeExchange  = publishExchangeDeclareStrategy.DeclareExchange(advancedBus, scheduleMeType, ExchangeType.Topic);
            var baseMessageType     = typeof(T);
            var concreteMessageType = message.GetType();
            var serializedMessage   = messageSerializationStrategy.SerializeMessage(new Message <T>(message)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(concreteMessageType)
                }
            });
            var scheduleMe = new ScheduleMe
            {
                WakeTime          = futurePublishDate,
                CancellationKey   = cancellationKey,
                InnerMessage      = serializedMessage.Body,
                MessageProperties = serializedMessage.Properties,
                BindingKey        = typeNameSerializer.Serialize(typeof(T)),
                ExchangeType      = ExchangeType.Topic,
                Exchange          = conventions.ExchangeNamingConvention(baseMessageType),
                RoutingKey        = "#"
            };
            var easyNetQMessage = new Message <ScheduleMe>(scheduleMe)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(scheduleMeType)
                }
            };

            advancedBus.Publish(scheduleMeExchange, conventions.TopicNamingConvention(scheduleMeType), false, easyNetQMessage);
        }
        /// <summary>
        /// Send the message to the exchange it was ultimately destined for
        /// </summary>
        /// <param name="wrappedMessage"></param>
        private void Publish(ScheduleMe wrappedMessage)
        {
            try
            {
                var bindingKey = wrappedMessage.BindingKey;
                var message    = wrappedMessage.InnerMessage;

                log.DebugWrite(string.Format(
                                   "Publishing message to '{0}'", bindingKey));

                var exchange = bus.Advanced.ExchangeDeclare(bindingKey, ExchangeType.Topic);
                bus.Advanced.Publish(
                    exchange,
                    bindingKey,
                    false,
                    false,
                    new MessageProperties {
                    Type = bindingKey
                },
                    message);
            }
            catch (Exception ex)
            {
                log.ErrorWrite("Error in publish \r\n{0}", ex);
            }
        }
Ejemplo n.º 4
0
        public Task FuturePublishAsync <T>(DateTime futurePublishDate, string cancellationKey, T message) where T : class
        {
            Preconditions.CheckNotNull(message, "message");
            var messageType = typeof(ScheduleMe);

            return(publishExchangeDeclareStrategy.DeclareExchangeAsync(advancedBus, messageType, ExchangeType.Topic).Then(exchange =>
            {
                var typeName = typeNameSerializer.Serialize(typeof(T));
                var messageBody = serializer.MessageToBytes(message);
                var scheduleMe = new ScheduleMe
                {
                    WakeTime = futurePublishDate,
                    BindingKey = typeName,
                    CancellationKey = cancellationKey,
                    InnerMessage = messageBody
                };
                var easyNetQMessage = new Message <ScheduleMe>(scheduleMe)
                {
                    Properties =
                    {
                        DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(messageType)
                    }
                };
                return advancedBus.PublishAsync(exchange, conventions.TopicNamingConvention(messageType), false, false, easyNetQMessage);
            }));
        }
Ejemplo n.º 5
0
        public void Store(ScheduleMe scheduleMe)
        {
            WithStoredProcedureCommand(insertSql, command =>
            {
                command.Parameters.AddWithValue("@WakeTime", scheduleMe.WakeTime);
                command.Parameters.AddWithValue("@BindingKey", scheduleMe.BindingKey);
                command.Parameters.AddWithValue("@Message", scheduleMe.InnerMessage);

                command.ExecuteNonQuery();
            });
        }
Ejemplo n.º 6
0
        public void Store(ScheduleMe scheduleMe)
        {
            WithStoredProcedureCommand(dialect.InsertProcedureName, command =>
            {
                AddParameter(command, dialect.WakeTimeParameterName, scheduleMe.WakeTime, DbType.DateTime);
                AddParameter(command, dialect.BindingKeyParameterName, scheduleMe.BindingKey, DbType.String);
                AddParameter(command, dialect.MessageParameterName, scheduleMe.InnerMessage, DbType.Binary);

                command.ExecuteNonQuery();
            });
        }
Ejemplo n.º 7
0
 private void OnMessage(ScheduleMe message)
 {
     log.DebugWrite("Got Schedule Message");
     scheduleRepository.Store(new Schedule
     {
         Id = Guid.NewGuid(),
         CancellationKey = message.CancellationKey,
         BindingKey      = message.BindingKey,
         InnerMessage    = message.InnerMessage,
         State           = ScheduleState.Pending,
         WakeTime        = message.WakeTime
     });
 }
        /// <summary>
        /// Returns a new expiry time span in milliseconds for the message.
        /// If the milliseconds exceeds Int32.MaxValue, returns MaxValue.
        /// If the expiry time is past, returns 0.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private int TimeToPublish(ScheduleMe message)
        {
            TimeSpan expirySpan = message.WakeTime.Subtract(DateTime.Now);
            double   expiry     = expirySpan.TotalMilliseconds;

            if (expiry > Int32.MaxValue)
            {
                expiry = Int32.MaxValue;
            }
            if (expiry < 0)
            {
                expiry = 0;
            }

            return(Convert.ToInt32(expiry));
        }
Ejemplo n.º 9
0
 private void OnMessage(ScheduleMe message)
 {
     log.DebugWrite("Got Schedule Message");
     scheduleRepository.Store(new Schedule
     {
         Id = Guid.NewGuid(),
         CancellationKey = message.CancellationKey,
         BindingKey      = message.BindingKey,
         InnerMessage    = message.InnerMessage,
         State           = ScheduleState.Pending,
         WakeTime        = message.WakeTime,
         Exchange        = message.Exchange,
         ExchangeType    = message.ExchangeType,
         RoutingKey      = message.RoutingKey,
         BasicProperties = message.MessageProperties
     });
 }
Ejemplo n.º 10
0
        public void Store(ScheduleMe scheduleMe)
        {
            WithStoredProcedureCommand(dialect.InsertProcedureName, command =>
            {
                AddParameter(command, dialect.WakeTimeParameterName, scheduleMe.WakeTime, DbType.DateTime);
                AddParameter(command, dialect.BindingKeyParameterName, scheduleMe.BindingKey, DbType.String);
                AddParameter(command, dialect.CancellationKeyParameterName, scheduleMe.CancellationKey, DbType.String);
                AddParameter(command, dialect.MessageParameterName, scheduleMe.InnerMessage, DbType.Binary);
                AddParameter(command, dialect.ExchangeParameterName, scheduleMe.Exchange, DbType.String);
                AddParameter(command, dialect.ExchangeTypeParameterName, scheduleMe.ExchangeType, DbType.String);
                AddParameter(command, dialect.RoutingKeyParameterName, scheduleMe.RoutingKey, DbType.String);
                AddParameter(command, dialect.MessagePropertiesParameterName, SerializeToString(scheduleMe.MessageProperties), DbType.String);
                AddParameter(command, dialect.InstanceNameParameterName, configuration.InstanceName, DbType.String);

                command.ExecuteNonQuery();
            });
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Put the message back into the ScheduleMe_Pending queue
        /// </summary>
        /// <param name="message"></param>
        /// <param name="queueFor"></param>
        private void ReQueue(ScheduleMe message, int expiry)
        {
            try
            {
                var advancedBus = bus.Advanced;
                var conventions = advancedBus.Container.Resolve <IConventions>();

                var                   exchangeName = conventions.ExchangeNamingConvention(ScheduleMeType) + "_Pending";
                IExchange             exchange     = advancedBus.ExchangeDeclare(exchangeName, ExchangeType.Topic);
                IMessage <ScheduleMe> newMessage   = new Message <ScheduleMe>(message);
                newMessage.Properties.Expiration = expiry.ToString();

                log.DebugWrite(string.Format("Requeuing message for {0}", FriendlyTimeSpan(expiry)));

                advancedBus.Publish(exchange, "#", false, false, newMessage);
            }
            catch (Exception ex)
            {
                log.ErrorWrite("Error in requeue \r\n{0}", ex);
            }
        }
Ejemplo n.º 12
0
 public void OnMessage(ScheduleMe scheduleMe)
 {
     log.DebugWrite("Got Schedule Message");
     scheduleRepository.Store(scheduleMe);
 }
Ejemplo n.º 13
0
 public void Store(ScheduleMe scheduleMe)
 {
     throw new NotImplementedException();
 }