static void SetObjectProperties(ScheduledMessageData job, global::Quartz.JobDataMap jobData) { foreach (var key in jobData.Keys) { if (TypeCache <ScheduledMessageData> .ReadWritePropertyCache.TryGetProperty(key, out ReadWriteProperty <ScheduledMessageData> property)) { var value = jobData[key]; if (property.Property.PropertyType == typeof(Uri)) { value = new Uri(value.ToString()); } property.Set(job, value); } } }
public async Task Execute(global::Quartz.IJobExecutionContext context) { var messageData = new ScheduledMessageData(); var jobData = new global::Quartz.JobDataMap(); jobData.PutAll(context.Scheduler.Context); jobData.PutAll(context.JobDetail.JobDataMap); jobData.PutAll(context.Trigger.JobDataMap); jobData.Put("PayloadMessageHeadersAsJson", CreatePayloadHeaderString(context)); SetObjectProperties(messageData, jobData); try { var bus = (IBus)context.Scheduler.Context[BUS_CONTEXT_KEY] ?? _bus; if (bus == null) { throw new Exception("Could not find MassTransit Bus instance on the Job or the Scheduler Context."); } var destinationAddress = messageData.Destination; var sourceAddress = bus.Address; IPipe <SendContext> sendPipe = CreateMessageContext(messageData, sourceAddress); var endpoint = await bus.GetSendEndpoint(destinationAddress).ConfigureAwait(false); var scheduled = new Scheduled(); await endpoint.Send(scheduled, sendPipe, context.CancellationToken).ConfigureAwait(false); LogContext.Debug?.Log("Schedule Executed: {Key} {Schedule}", context.JobDetail.Key, context.Trigger.GetNextFireTimeUtc()); } catch (Exception ex) { LogContext.Error?.Log(ex, "Failed to send scheduled message, type: {MessageType}, destination: {DestinationAddress}", messageData.MessageType, messageData.Destination); throw new global::Quartz.JobExecutionException(ex, context.RefireCount < 5); } }