public async Task Consume(ConsumeContext <global::MassTransit.Scheduling.ScheduleMessage> context) { var correlationId = context.Message.CorrelationId.ToString("N"); var jobKey = new global::Quartz.JobKey(correlationId); var jobDetail = await CreateJobDetail(context, context.Message.Destination, jobKey, context.Message.CorrelationId).ConfigureAwait(false); var triggerKey = new global::Quartz.TriggerKey(correlationId); var trigger = global::Quartz.TriggerBuilder.Create() .ForJob(jobDetail) .StartAt(context.Message.ScheduledTime) .WithSchedule(global::Quartz.SimpleScheduleBuilder.Create().WithMisfireHandlingInstructionFireNow()) .WithIdentity(triggerKey) .Build(); var scheduler = _scheduler ??= await _schedulerTask.ConfigureAwait(false); if (await scheduler.CheckExists(trigger.Key, context.CancellationToken).ConfigureAwait(false)) { await scheduler.UnscheduleJob(trigger.Key, context.CancellationToken).ConfigureAwait(false); } await scheduler.ScheduleJob(jobDetail, trigger, context.CancellationToken).ConfigureAwait(false); LogContext.Debug?.Log("Scheduled: {Key} {Schedule}", jobKey, trigger.GetNextFireTimeUtc()); }
public async Task Consume(ConsumeContext <global::MassTransit.Scheduling.ScheduleRecurringMessage> context) { var jobKey = new global::Quartz.JobKey(context.Message.Schedule.ScheduleId, context.Message.Schedule.ScheduleGroup); var jobDetail = await CreateJobDetail(context, context.Message.Destination, jobKey).ConfigureAwait(false); var triggerKey = new global::Quartz.TriggerKey("Recurring.Trigger." + context.Message.Schedule.ScheduleId, context.Message.Schedule.ScheduleGroup); var trigger = CreateTrigger(context.Message.Schedule, jobDetail, triggerKey); var scheduler = _scheduler ??= await _schedulerTask.ConfigureAwait(false); if (await scheduler.CheckExists(triggerKey, context.CancellationToken).ConfigureAwait(false)) { await scheduler.UnscheduleJob(triggerKey, context.CancellationToken).ConfigureAwait(false); } await scheduler.ScheduleJob(jobDetail, trigger, context.CancellationToken).ConfigureAwait(false); LogContext.Debug?.Log("Scheduled: {Key} {Schedule}", jobKey, trigger.GetNextFireTimeUtc()); }
static async Task <global::Quartz.IJobDetail> CreateJobDetail(ConsumeContext context, Uri destination, global::Quartz.JobKey jobKey, Guid?tokenId = default) { var body = Encoding.UTF8.GetString(context.ReceiveContext.GetBody()); var mediaType = context.ReceiveContext.ContentType?.MediaType; if (JsonMessageSerializer.JsonContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase)) { body = TranslateJsonBody(body, destination.ToString()); } else if (XmlMessageSerializer.XmlContentType.MediaType.Equals(mediaType, StringComparison.OrdinalIgnoreCase)) { body = TranslateXmlBody(body, destination.ToString()); } else { throw new InvalidOperationException("Only JSON and XML messages can be scheduled"); } var builder = global::Quartz.JobBuilder.Create <NativeScheduledMessageJob>() .RequestRecovery() .WithIdentity(jobKey) .UsingJobData("Destination", ToString(destination)) .UsingJobData("ResponseAddress", ToString(context.ResponseAddress)) .UsingJobData("FaultAddress", ToString(context.FaultAddress)) .UsingJobData("Body", body) .UsingJobData("ContentType", mediaType); if (context.MessageId.HasValue) { builder = builder.UsingJobData("MessageId", context.MessageId.Value.ToString()); } if (context.CorrelationId.HasValue) { builder = builder.UsingJobData("CorrelationId", context.CorrelationId.Value.ToString()); } if (context.ConversationId.HasValue) { builder = builder.UsingJobData("ConversationId", context.ConversationId.Value.ToString()); } if (context.InitiatorId.HasValue) { builder = builder.UsingJobData("InitiatorId", context.InitiatorId.Value.ToString()); } if (context.RequestId.HasValue) { builder = builder.UsingJobData("RequestId", context.RequestId.Value.ToString()); } if (context.ExpirationTime.HasValue) { builder = builder.UsingJobData("ExpirationTime", context.ExpirationTime.Value.ToString("O")); } if (tokenId.HasValue) { builder = builder.UsingJobData("TokenId", tokenId.Value.ToString("N")); } IEnumerable <KeyValuePair <string, object> > headers = context.Headers.GetAll(); if (headers.Any()) { builder = builder.UsingJobData("HeadersAsJson", JsonConvert.SerializeObject(headers)); } var jobDetail = builder .Build(); return(jobDetail); }