public async Task <ScheduledRecurringMessage> ScheduleRecurringSend(IRecurringMessageScheduler scheduler, Uri destinationAddress,
                                                                            RecurringSchedule schedule, object message, CancellationToken cancellationToken)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }
            if (schedule == null)
            {
                throw new ArgumentNullException(nameof(schedule));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var msg = message as T;

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

            ScheduledRecurringMessage <T> scheduleSend = await scheduler.ScheduleRecurringSend(destinationAddress, schedule, msg, cancellationToken)
                                                         .ConfigureAwait(false);

            return(scheduleSend);
        }
Example #2
0
        public async Task Should_contain_additional_headers_that_provide_time_domain_context()
        {
            var scheduleId = Guid.NewGuid().ToString();

            await Scheduler.ScheduleSend(InputQueueAddress, DateTime.UtcNow + TimeSpan.FromSeconds(10), new Done { Name = "Joe" });

            ScheduledRecurringMessage <Interval> scheduledRecurringMessage =
                await QuartzEndpoint.ScheduleRecurringSend(InputQueueAddress, new MySchedule(), new Interval { Name = "Joe" });

            await _done;

            Assert.Greater(_count, 0, "Expected to see at least one interval");


            Assert.IsNotNull(_lastInterval.Headers.Get <DateTimeOffset>(MessageHeaders.Quartz.Scheduled, null));
            Assert.IsNotNull(_lastInterval.Headers.Get <DateTimeOffset>(MessageHeaders.Quartz.Sent, null));
            Assert.IsNotNull(_lastInterval.Headers.Get <DateTimeOffset>(MessageHeaders.Quartz.NextScheduled, null));
            Assert.IsNotNull(_lastInterval.Headers.Get <DateTimeOffset>(MessageHeaders.Quartz.PreviousSent, null));

            Console.WriteLine("{0}", _lastInterval.Headers.Get <DateTimeOffset>(MessageHeaders.Quartz.NextScheduled, null));
        }
Example #3
0
        public async Task Should_cancel_recurring_schedule()
        {
            var scheduleId = Guid.NewGuid().ToString();

            await Scheduler.ScheduleSend(InputQueueAddress, DateTime.UtcNow + TimeSpan.FromSeconds(10), new Done { Name = "Joe" });

            ScheduledRecurringMessage <Interval> scheduledRecurringMessage =
                await QuartzEndpoint.ScheduleRecurringSend(InputQueueAddress, new MyCancelableSchedule(scheduleId), new Interval { Name = "Joe" });

            await _done;

            var countBeforeCancel = _count;

            Assert.AreEqual(8, _count, "Expected to see 8 interval messages");

            await Bus.CancelScheduledRecurringSend(scheduledRecurringMessage);

            await Scheduler.ScheduleSend(InputQueueAddress, DateTime.UtcNow + TimeSpan.FromSeconds(10), new DoneAgain { Name = "Joe" });

            await _doneAgain;

            Assert.AreEqual(countBeforeCancel, _count, "Expected to see the count matches.");
        }
        /// <summary>
        /// Cancel a scheduled message using the scheduled message instance
        /// </summary>
        /// <param name="endpoint">The endpoint of the scheduling service</param>
        /// <param name="message">The schedule message reference</param>
        public static Task CancelScheduledRecurringSend <T>(this IPublishEndpoint endpoint, ScheduledRecurringMessage <T> message)
            where T : class
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            return(CancelScheduledRecurringSend(endpoint, message.Schedule.ScheduleId, message.Schedule.ScheduleGroup));
        }
        public static async void Start()
        {
            // load the Log4Net config from app.config
            XmlConfigurator.Configure();

            // create the scheduler
            var scheduler = CreateScheduler();

            // configure the bus using the scheduler
            var busControl = ConfigureBus(scheduler);

            // set the Quartz JobFactory, that will give the scheduler the ability to create MT jobs
            scheduler.JobFactory = new MassTransitJobFactory(busControl, new SimpleJobFactory());

            // now start the scheduler
            await scheduler.Start();

            busControl.Start();
            Console.WriteLine("'q' to exit");
            Console.WriteLine("'1' -> Scheduling a message from a consumer");
            Console.WriteLine("'2' -> Scheduling a message from the bus");
            Console.WriteLine("'3' -> Scheduling a recurring message");
            Console.WriteLine("'4' -> Cancel Scheduling a recurring message");
            do
            {
                var value = Console.ReadLine();

                if ("q".Equals(value, StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                switch (value)
                {
                case "1":
                    /*
                     * Scheduling a message from a consumer
                     * Push a IScheduleNotification message to the schedule_notification_queue
                     * The consumer will trigger a scheduled send
                     */
                    Console.Write("Sending IScheduleNotification");
                    await busControl.GetSendEndpoint(new Uri("rabbitmq://localhost/schedule_notification_queue"))
                    .Result.Send <IScheduleNotification>(new
                    {
                        DeliveryTime = DateTime.Now.AddSeconds(5),
                        EmailAddress = "*****@*****.**",
                        Body         = "Hello World!"
                    });

                    break;

                case "2":
                    /*
                     * Scheduling a message from the bus
                     * Sends a SendNotificationCommand message to the notification_queue
                     * scheduled for 5 seconds later
                     */
                    Console.WriteLine("Sending SendNotificationCommand in 5 seconds");
                    await busControl.CreateMessageScheduler(new Uri("rabbitmq://localhost/notification_queue"))
                    .ScheduleSend(new Uri("rabbitmq://localhost/notification_queue"),
                                  TimeSpan.FromSeconds(5),
                                  new ScheduleNotificationConsumer.SendNotificationCommand
                    {
                        EmailAddress = "*****@*****.**",
                        Body         = "Hello World!",
                    });

                    break;

                case "3":
                    /*
                     * Scheduling a recurring message
                     * Sends a SendNotificationCommand message to the notification_queue
                     * scheduled recurring evry 5 seconds
                     */
                    Console.WriteLine("Sending SendNotificationCommand every 5 seconds");
                    _recurringScheduledMessage = await busControl.GetSendEndpoint(SchedulerAddress)
                                                 .Result.ScheduleRecurringSend(new Uri("rabbitmq://localhost/notification_queue"),
                                                                               new PollExternalSystemSchedule(),
                                                                               new ScheduleNotificationConsumer.SendNotificationCommand
                    {
                        EmailAddress = "*****@*****.**",
                        Body         = "Hello World!",
                    });

                    break;

                case "4":
                    /*
                     * Cancel Scheduling the recurring message
                     * todo: Cancelling a recurring send doesn't work! Why?
                     * An exchange "MassTransit.Scheduling:CancelScheduledRecurringMessage" is created
                     * but without any binding.
                     * When cancelling a message is pushed in this exchange -> without any effect
                     */
                    if (_recurringScheduledMessage != null)
                    {
                        Console.WriteLine("Cancel sending SendNotificationCommand every 5 seconds");

                        await busControl.CancelScheduledRecurringSend(_recurringScheduledMessage);

                        _recurringScheduledMessage = null;
                    }
                    else
                    {
                        Console.WriteLine("No schedule to cancel, please press 3 before");
                    }

                    break;
                }
            } while (true);

            await scheduler.Shutdown();

            busControl.Stop();
        }
 public static async Task CancelScheduledRecurringMessage <TMessage>(this IBus bus, ScheduledRecurringMessage <TMessage> recurringMessage)
     where TMessage : class
 {
     await bus.CancelScheduledRecurringSend(recurringMessage);
 }