我正在将crystal.net 3.0.7设置为我的.net core 2.2应用程序。如果我仅将一项工作添加到“服务”中,则一切工作都将顺利进行。但是,当我添加第二份工作时。第一个作业的触发以某种方式触发了第二个作业。
我对石英的扩展方法:
public static void AddQuartz(this IServiceCollection services, string configuration)
{
services.AddSingleton<IJobFactory, ScheduledJobFactory>();
var properties = new NameValueCollection
{
["quartz.serializer.type"] = "json",
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
["quartz.jobStore.useProperties"] = "false",
["quartz.jobStore.dataSource"] = "sqlserver",
["quartz.jobStore.tablePrefix"] = "QRTZ_",
["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
//["quartz.dataSource.sqlserver.provider"] = "SqlServer-41", // SqlServer-41 is the new provider for .NET Core
["quartz.dataSource.sqlserver.provider"] = "SqlServer",
["quartz.dataSource.sqlserver.connectionString"] = configuration
};
services.AddSingleton<IScheduler>(provider =>
{
var schedulerFactory = new StdSchedulerFactory(properties);
var scheduler = schedulerFactory.GetScheduler().Result;
scheduler.JobFactory = provider.GetService<IJobFactory>();
//scheduler.ScheduleJob(JobTrigger);
return scheduler;
});
}
public static void AddJob(this IServiceCollection services, Type job)
{
services.Add(new ServiceDescriptor(typeof(IJob), job, ServiceLifetime.Singleton));
}
public static void StartRepetitiveJob<TJob>(this IScheduler scheduler, TimeSpan runInterval, DateTimeOffset startWhen) where TJob : IJob
{
var jobName = typeof(TJob).FullName;
var job = JobBuilder.Create<TJob>()
.WithIdentity(jobName,jobName)
.StoreDurably()
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity($"{jobName}.trigger", $"{jobName}.trigger")
.StartAt(startWhen)
.WithSimpleSchedule(scheduleBuilder =>
scheduleBuilder
.WithInterval(runInterval)
.RepeatForever())
.ForJob(job)
.Build();
scheduler.ScheduleJob(job, trigger);
}
public static void FireJobOnlyOnce<TJob>(this IScheduler scheduler, DateTime fireat, IDictionary<string, object> dictionary) where TJob : IJob
{
var jobName = typeof(TJob).FullName;
var jobDataMap = new JobDataMap(dictionary);
var job = JobBuilder.Create<TJob>()
.WithIdentity(jobName,jobName)
//.WithIdentity(jobName, Guid.NewGuid().ToString())
.SetJobData(jobDataMap)
.StoreDurably()
.Build();
var trigger = (ISimpleTrigger) TriggerBuilder.Create()
.WithIdentity($"{jobName}.trigger", $"{jobName}.trigger")
.StartAt(fireat.ToUniversalTime())
.UsingJobData(jobDataMap)
.ForJob(job)
.Build();
scheduler.ScheduleJob(job, trigger);
}
在 startup.cs 中:
services.AddQuartz(Configuration.GetConnectionString("DefaultConnection"));
services.AddJob(typeof(CheckPropertyListingJob));
services.AddJob(typeof(ScheduledGetPropertyDataJob));
在 program.cs 中:
DateTime dt = DateTime.UtcNow;
TimeSpan interval = TimeSpan.FromHours(6);
scheduler.StartRepetitiveJob<CheckPropertyListingJob>(interval, new DateTimeOffset(dt.Add(TimeSpan.FromMinutes(2)), TimeSpan.Zero));
因此,当我仅添加CheckPropertyListingJob
时,一切都很好。但是,当我以某种方式添加ScheduledGetPropertyDataJob
时,为CheckPropertyListingJob
创建的触发器会触发其他作业。
答案 0 :(得分:0)
我终于解决了这个问题。我仅按照一份教程就开始实施石英。所以我的ScheduledJobFactory看起来像这样:
public class ScheduledJobFactory: IJobFactory
{
private readonly IServiceProvider serviceProvider;
public ScheduledJobFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
return serviceProvider.GetService(typeof(IJob)) as IJob;
}
public void ReturnJob(IJob job)
{
var disposable = job as IDisposable;
disposable?.Dispose();
}
}
我根据此answer编辑了NewJob方法,一切都很好。我简直不敢相信我会如此无知。
return (IJob)serviceProvider.GetService(bundle.JobDetail.JobType);