简体   繁体   中英

Quartz.net Manually trigger job from Web API

I realize this has sort of been asked before but I want to get a clear confirmation.

I have a Windows Service running the Quartz.Net Scheduler. Jobs and Triggers have been created.

We will have an angular web client that will at times, need to fire jobs manually.

So in a Web API Controller, I have code like this:

            var properties = new NameValueCollection
            {
                ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
                ["quartz.jobStore.useProperties"] = "true",
                ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
                ["quartz.jobStore.dataSource"] = "myDS",
                ["quartz.jobStore.tablePrefix"] = "QRTZ_",
                ["quartz.dataSource.NAME.provider"] = "SqlServer",
                ["quartz.dataSource.NAME.connectionString"] = "Server=localhost;Database=QuartzScheduler;Uid=blahuser;Pwd=blahpwd",
                ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"
            };

            var sched = new StdSchedulerFactory(properties).GetScheduler().Result;

            var jobKey = new JobKey("DailyJob1130EST", "DailyGroup");
            var jobDataMap = new JobDataMap();
            jobDataMap.Add("listIds", "33333");

            sched.TriggerJob(jobKey, jobDataMap);

The Job Name and Group do exist in the database.

I was hoping that the call to TriggerJob would cause the Scheduler I have running in my windows service, to fire the job. But it doesn't. Nothing happens, not even an error.

BTW, I don't want to use remoting since it requires the full .NET Framework and the help docs say that it is considered unsafe.

If TriggerJob doesn't work, I guess to run a job manually I'd have to add a new trigger to the scheduler to run once, or something???

There may be other ways, but one way that I was able to successfully do it was:

            var sched = await new StdSchedulerFactory(properties).GetScheduler();

            var jobKey = new JobKey("DailyJob1130EST", "DailyGroup");
            var jobDataMap = new JobDataMap();
            jobDataMap.Add("listIds", rInt.ToString());

            var trig = TriggerBuilder.Create()
                .WithIdentity("RunNowTrigger")
                .StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(5)))
                .WithDescription("Run Now Trigger")
                .Build();

            sched.TriggerJob(jobKey, jobDataMap);

Note: "properties" were my NameValueCollection config information, which I omitted from the sample code. It was nothing out of the ordinary. It just setup the jobStore, dataSource, serializer.type and threadPool.type settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM