Exemplo n.º 1
0
        /// <summary>
        ///     Sends an object as a message, using the type of the message instance.
        /// </summary>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message object</param>
        /// <param name="destinationAddress">The destination address where the schedule message should be sent</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Send is acknowledged by the broker</returns>
        public static Task <ScheduledMessage> ScheduleSend(this IMessageScheduler scheduler, Uri destinationAddress, TimeSpan delay, object message,
                                                           CancellationToken cancellationToken = default(CancellationToken))
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.ScheduleSend(destinationAddress, scheduledTime, message, cancellationToken));
        }
Exemplo n.º 2
0
        public async Task <ScheduledMessage> ScheduleSend(IMessageScheduler scheduler, Uri destinationAddress, DateTime scheduledTime, object message,
                                                          IPipe <SendContext> pipe, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (pipe == null)
            {
                throw new ArgumentNullException(nameof(pipe));
            }

            var msg = message as T;

            if (msg == null)
            {
                throw new ArgumentException("Unexpected message type: " + message.GetType().GetTypeName());
            }

            ScheduledMessage <T> scheduleSend =
                await scheduler.ScheduleSend(destinationAddress, scheduledTime, msg, pipe, cancellationToken).ConfigureAwait(false);

            return(scheduleSend);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Publishes an object as a message, using the message type specified. If the object cannot be cast
        /// to the specified message type, an exception will be thrown.
        /// </summary>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message object</param>
        /// <param name="messageType">The type of the message (use message.GetType() if desired)</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage> SchedulePublish(this IMessageScheduler scheduler, TimeSpan delay, object message,
                                                              Type messageType, CancellationToken cancellationToken = default)
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish(scheduledTime, message, messageType, cancellationToken));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Publishes an interface message, initializing the properties of the interface using the anonymous
        /// object specified
        /// </summary>
        /// <typeparam name="T">The interface type to send</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="values">The property values to initialize on the interface</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > SchedulePublish <T>(this IMessageScheduler scheduler, TimeSpan delay, object values,
                                                                       CancellationToken cancellationToken = default)
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish <T>(scheduledTime, values, cancellationToken));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Publish a message
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="pipe"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Publish is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > SchedulePublish <T>(this IMessageScheduler scheduler, TimeSpan delay, T message,
                                                                       IPipe <SendContext> pipe, CancellationToken cancellationToken = default)
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.SchedulePublish(scheduledTime, message, pipe, cancellationToken));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Send a message
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The message</param>
        /// <param name="destinationAddress">The destination address where the schedule message should be sent</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="pipe"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Send is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > ScheduleSend <T>(this IMessageScheduler scheduler, Uri destinationAddress, TimeSpan delay, T message,
                                                                    IPipe <SendContext <T> > pipe, CancellationToken cancellationToken = default(CancellationToken))
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.ScheduleSend(destinationAddress, scheduledTime, message, pipe, cancellationToken));
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Sends an interface message, initializing the properties of the interface using the anonymous
        ///     object specified
        /// </summary>
        /// <typeparam name="T">The interface type to send</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="values">The property values to initialize on the interface</param>
        /// <param name="destinationAddress">The destination address where the schedule message should be sent</param>
        /// <param name="delay">The time at which the message should be delivered to the queue</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The task which is completed once the Send is acknowledged by the broker</returns>
        public static Task <ScheduledMessage <T> > ScheduleSend <T>(this IMessageScheduler scheduler, Uri destinationAddress, TimeSpan delay, object values,
                                                                    CancellationToken cancellationToken = default(CancellationToken))
            where T : class
        {
            var scheduledTime = DateTime.UtcNow + delay;

            return(scheduler.ScheduleSend <T>(destinationAddress, scheduledTime, values, cancellationToken));
        }
Exemplo n.º 8
0
        public BenchmarkSchedulerService(MessageMetricCapture capture, MessageLatencySettings settings, IMessageScheduler scheduler,
                                         IEndpointNameFormatter endpointNameFormatter, IHostApplicationLifetime lifetime)
        {
            _capture   = capture;
            _settings  = settings;
            _scheduler = scheduler;
            _lifetime  = lifetime;

            _address = new Uri($"exchange:{endpointNameFormatter.Consumer<MessageLatencyConsumer>()}");
        }
Exemplo n.º 9
0
 public void SendToastNotification(Guid subscriptionId, Guid notificationId, string title, string body, int attemptsLeft, INotificationSender notificationSender, IMessageScheduler messageScheduler)
 {
     Subscription subscription;
     if (!_subscriptions.TryGetValue(subscriptionId, out subscription))
     {
         throw new UnknownSubscriptionException(Id, subscriptionId);
     }
     if (subscription.Kind != NotificationKind.Toast)
     {
         throw new InvalidOperationException("Trying to send Toast notification to Tile/Raw subscription.");
     }
     SendNotification(subscriptionId, new ToastNotification(notificationId, title, body, subscription.ParamUri), attemptsLeft, notificationSender, messageScheduler);
 }
Exemplo n.º 10
0
 public void SendRawNotification(Guid subscriptionId, Guid notificationId, string rawContent, int attemptsLeft, INotificationSender notificationSender, IMessageScheduler messageScheduler)
 {
     Subscription subscription;
     if (!_subscriptions.TryGetValue(subscriptionId, out subscription))
     {
         throw new UnknownSubscriptionException(Id, subscriptionId);
     }
     if (subscription.Kind != NotificationKind.Raw)
     {
         throw new InvalidOperationException("Trying to send Raw notification to Tile/Toast subscription.");
     }
     SendNotification(subscriptionId, new RawNotification(notificationId, rawContent), attemptsLeft, notificationSender, messageScheduler);
 }
Exemplo n.º 11
0
        public void SendTileNotification(Guid subscriptionId, Guid notificationId, string title, int counter, string backBackgroundUri, string backTitle, string backgroundUri, string backContent, int attemptsLeft, INotificationSender notificationSender, IMessageScheduler messageScheduler)
        {
            Subscription subscription;
            if (!_subscriptions.TryGetValue(subscriptionId, out subscription))
            {
                throw new UnknownSubscriptionException(Id, subscriptionId);
            }
            if (subscription.Kind != NotificationKind.Tile)
            {
                throw new InvalidOperationException("Trying to send Tile notification to Toast/Raw subscription.");
            }

            SendNotification(subscriptionId, new TileNotification(notificationId, title, counter, backBackgroundUri, backTitle, backgroundUri, backContent, subscription.ParamUri), attemptsLeft, notificationSender, messageScheduler);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Cancel a scheduled message
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="scheduler">The message scheduler</param>
        /// <param name="message">The </param>
        /// <returns></returns>
        public static Task CancelScheduledSend <T>(this IMessageScheduler scheduler, ScheduledMessage <T> message)
            where T : class
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            return(scheduler.CancelScheduledSend(message.Destination, message.TokenId));
        }
Exemplo n.º 13
0
        public ConsumeMessageSchedulerContext(ConsumeContext consumeContext, IMessageScheduler scheduler)
        {
            if (consumeContext == null)
            {
                throw new ArgumentNullException(nameof(consumeContext));
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            _consumeContext = consumeContext;
            _scheduler      = scheduler;
        }
        public async Task <ScheduledMessage> ScheduleSend(IMessageScheduler scheduler, Uri destinationAddress, DateTime scheduledTime, object message,
                                                          CancellationToken cancellationToken = default)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message is T msg)
            {
                return(await scheduler.ScheduleSend(destinationAddress, scheduledTime, msg, cancellationToken).ConfigureAwait(false));
            }

            throw new ArgumentException("Unexpected message type: " + TypeMetadataCache.GetShortName(message.GetType()));
        }
Exemplo n.º 15
0
        private void SendNotification(Guid subscriptionId, NotificationBase notification, int attemptsLeft, INotificationSender notificationSender, IMessageScheduler messageScheduler)
        {
            var response = notificationSender.SendNotification(_notificationChannel, notification);

            var toastNotification = notification as ToastNotification;
            var rawNotification = notification as RawNotification;
            var tileNotification = notification as TileNotification;

            if (response.HttpStatusCode == 200)
            {
                if (response.NotificationStatus == "Received")
                {
                    if (toastNotification != null)
                        PublishEvent(new ToastNotificationSucceededEvent(subscriptionId, toastNotification.NotificationId, attemptsLeft, toastNotification.Title, toastNotification.Body));
                    else if (rawNotification != null)
                        PublishEvent(new RawNotificationSucceededEvent(subscriptionId, rawNotification.NotificationId, attemptsLeft, rawNotification.RawContent));
                    else if (tileNotification != null)
                        PublishEvent(new TileNotificationSucceededEvent(subscriptionId, tileNotification.NotificationId, attemptsLeft, tileNotification.Title, tileNotification.BackgroundUri, tileNotification.Counter, tileNotification.BackTitle, tileNotification.BackContent, tileNotification.BackBackgroundUri));
                    return;
                }
            }

            // Something went wrong somehow. First, let's publish an event saing so.
            if (toastNotification != null)
                PublishEvent(new ToastNotificationFailedEvent(subscriptionId, toastNotification.NotificationId, attemptsLeft, DateTime.Now, response.HttpStatusCode, response.NotificationStatus, response.DeviceConnectionStatus, response.SubscriptionStatus, toastNotification.Title, toastNotification.Body));
            else if (rawNotification != null)
                PublishEvent(new RawNotificationFailedEvent(subscriptionId, rawNotification.NotificationId, attemptsLeft, DateTime.Now, response.HttpStatusCode, response.NotificationStatus, response.DeviceConnectionStatus, response.SubscriptionStatus, rawNotification.RawContent));
            else if (tileNotification != null)
                PublishEvent(new TileNotificationFailedEvent(subscriptionId, tileNotification.NotificationId, attemptsLeft, DateTime.Now, response.HttpStatusCode, response.NotificationStatus, response.DeviceConnectionStatus, response.SubscriptionStatus, tileNotification.Title, tileNotification.BackgroundUri, tileNotification.Counter, tileNotification.BackTitle, tileNotification.BackContent, tileNotification.BackBackgroundUri));

            // Next let's figure out what to do next
            var resendTime = DateTime.Now;
            var shouldAttemptAgain = false;

            if (response.NotificationStatus == "QueueFull"  || response.HttpStatusCode == 503)
            {
                // Service currently full or unavailable.
                resendTime = resendTime.AddMinutes(1); // TODO Increase exponentially
                shouldAttemptAgain = true;
            }
            else if (response.NotificationStatus == "Suppressed")
            {
                shouldAttemptAgain = false;
            }
            else if (response.NotificationStatus == "Dropped" && response.SubscriptionStatus == "Expired")
            {
                // Subscription Expired. Don't attempt again and unregister this client.
                shouldAttemptAgain = false;
                Unregister();
            }
            else if (response.NotificationStatus == "Dropped" && response.SubscriptionStatus == "Active")
            {
                // Rate limit reached. Attempt to resend in one hour
                resendTime = resendTime.AddHours(1);
                shouldAttemptAgain = true;
            }
            else if (response.NotificationStatus == "Dropped" && response.DeviceConnectionStatus == "Inactive")
            {
                resendTime = resendTime.AddHours(1);
                shouldAttemptAgain = true;
            }

            // Should we re-try?
            if (shouldAttemptAgain)
            {
                attemptsLeft--;
                if (toastNotification != null)
                    messageScheduler.ScheduleMessage(new SendToastNotificationCommand(this.Id, subscriptionId, toastNotification.NotificationId, toastNotification.Title, toastNotification.Body, attemptsLeft), resendTime);
                else if (rawNotification != null)
                    messageScheduler.ScheduleMessage(new SendRawNotificationCommand(this.Id, subscriptionId, rawNotification.NotificationId, rawNotification.RawContent, attemptsLeft), resendTime);
                else if (tileNotification != null)
                    messageScheduler.ScheduleMessage(new SendTileNotificationCommand(this.Id, subscriptionId, tileNotification.NotificationId, tileNotification.Title, tileNotification.Counter, tileNotification.BackgroundUri, tileNotification.BackTitle, tileNotification.BackContent, tileNotification.BackBackgroundUri, attemptsLeft), resendTime);
            }
        }
 public TimerEventListenerProcessor(ISubscriberRepository subscriberRepository, IMessageScheduler messageScheduler) : base(subscriberRepository)
 {
     _messageScheduler = messageScheduler;
 }
Exemplo n.º 17
0
 public ActiveMqMessageRedeliveryContext(ConsumeContext <TMessage> context, IMessageScheduler scheduler)
 {
     _context   = context;
     _scheduler = scheduler;
 }
 public TaskCreatedMessageHandler(TestDbContext dbContext, ILogger <TaskCreatedMessageHandler> logger, IMessageScheduler publishEndpoint)
 {
     _publishEndpoint = publishEndpoint;
     _dbContext       = dbContext;
     _logger          = logger;
 }
Exemplo n.º 19
0
 public WeatherForecastController(ILogger <WeatherForecastController> logger, IPublishEndpoint publishEndpoint, IMessageScheduler scheduler)
 {
     _logger          = logger;
     _publishEndpoint = publishEndpoint;
     _scheduler       = scheduler;
 }
 public static Task<ScheduledMessage> ScheduleSend(IMessageScheduler scheduler, Uri destinationAddress, DateTime scheduledTime, object message,
     Type messageType, IPipe<SendContext> pipe, CancellationToken cancellationToken)
 {
     return Cached.Converters.Value[messageType].ScheduleSend(scheduler, destinationAddress, scheduledTime, message, pipe, cancellationToken);
 }
 public static Task <ScheduledMessage> ScheduleSend(IMessageScheduler scheduler, Uri destinationAddress, DateTime scheduledTime, object message,
                                                    Type messageType, IPipe <SendContext> pipe, CancellationToken cancellationToken)
 {
     return(Cached.Converters.Value[messageType].ScheduleSend(scheduler, destinationAddress, scheduledTime, message, pipe, cancellationToken));
 }
Exemplo n.º 22
0
 public DelayedExchangeMessageRedeliveryContext(ConsumeContext <TMessage> context, IMessageScheduler scheduler)
 {
     _context   = context;
     _scheduler = scheduler;
 }
 public ConsumeMessageSchedulerContext(IMessageScheduler scheduler, Uri inputAddress)
 {
     _scheduler    = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
     _inputAddress = inputAddress ?? throw new ArgumentNullException(nameof(inputAddress));
 }