如何在 F# 中使用 Quartz.net
How to use Quartz.net with F#
我无法让 F# 根据 this 安排简单的调度程序。
open System
open Quartz
open Quartz.Impl
let schedulerFactory = StdSchedulerFactory()
let scheduler = schedulerFactory.GetScheduler()
scheduler.Start()
type Job () =
interface IJob with
member x.Execute(context: IJobExecutionContext) =
task{ //
Console.WriteLine(DateTime.Now)
}
let job = JobBuilder.Create<Job>().Build() //Exception Unhandled error!
let trigger =
TriggerBuilder.Create()
.WithSimpleSchedule(fun x ->
x.WithIntervalInSeconds(1).RepeatForever() |> ignore)
.Build()
scheduler.Result.ScheduleJob(job, trigger) |> ignore
Threading.Thread.Sleep(10000000)
Console.ReadKey()|>ignore
它给出行 let job = JobBuilder.Create<Job>().Build()
System.InvalidOperationException 的运行时错误:'Start may not be called on a task that has completed.'
我还不得不将 scheduler.ScheduleJob 更改为 scheduler.Result.ScheduleJob(.. 来自上面的原始示例。
根据 Bent 指出的文档,我认为应该这样做,使用 F# 6 中的 task
构建器:
open System
open System.Threading.Tasks
open Quartz
open Quartz.Impl
type Job() =
interface IJob with
member _.Execute(_context) =
Console.Out.WriteLineAsync(DateTime.Now.ToString())
task {
// Grab the Scheduler instance from the Factory
let factory = StdSchedulerFactory()
let! scheduler = factory.GetScheduler()
// and start it off
do! scheduler.Start()
// define the job and tie it to our Job class
let job =
JobBuilder.Create<Job>()
.WithIdentity("job1", "group1")
.Build()
// Trigger the job to run now, and then repeat every second
let trigger =
TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(fun x ->
x.WithIntervalInSeconds(1).RepeatForever()
|> ignore)
.Build()
// Tell quartz to schedule the job using our trigger
let! _offset = scheduler.ScheduleJob(job, trigger)
// some sleep to show what's happening
do! Task.Delay(TimeSpan.FromSeconds(10))
// and last shut down the scheduler when you are ready to close your program
do! scheduler.Shutdown()
} |> ignore
Console.ReadLine() |> ignore
输出类似于:
4/16/2022 6:11:52 PM
4/16/2022 6:11:52 PM
4/16/2022 6:11:53 PM
4/16/2022 6:11:54 PM
4/16/2022 6:11:55 PM
4/16/2022 6:11:56 PM
4/16/2022 6:11:57 PM
4/16/2022 6:11:58 PM
4/16/2022 6:11:59 PM
4/16/2022 6:12:00 PM
4/16/2022 6:12:01 PM
我无法让 F# 根据 this 安排简单的调度程序。
open System
open Quartz
open Quartz.Impl
let schedulerFactory = StdSchedulerFactory()
let scheduler = schedulerFactory.GetScheduler()
scheduler.Start()
type Job () =
interface IJob with
member x.Execute(context: IJobExecutionContext) =
task{ //
Console.WriteLine(DateTime.Now)
}
let job = JobBuilder.Create<Job>().Build() //Exception Unhandled error!
let trigger =
TriggerBuilder.Create()
.WithSimpleSchedule(fun x ->
x.WithIntervalInSeconds(1).RepeatForever() |> ignore)
.Build()
scheduler.Result.ScheduleJob(job, trigger) |> ignore
Threading.Thread.Sleep(10000000)
Console.ReadKey()|>ignore
它给出行 let job = JobBuilder.Create<Job>().Build()
System.InvalidOperationException 的运行时错误:'Start may not be called on a task that has completed.'
我还不得不将 scheduler.ScheduleJob 更改为 scheduler.Result.ScheduleJob(.. 来自上面的原始示例。
根据 Bent 指出的文档,我认为应该这样做,使用 F# 6 中的 task
构建器:
open System
open System.Threading.Tasks
open Quartz
open Quartz.Impl
type Job() =
interface IJob with
member _.Execute(_context) =
Console.Out.WriteLineAsync(DateTime.Now.ToString())
task {
// Grab the Scheduler instance from the Factory
let factory = StdSchedulerFactory()
let! scheduler = factory.GetScheduler()
// and start it off
do! scheduler.Start()
// define the job and tie it to our Job class
let job =
JobBuilder.Create<Job>()
.WithIdentity("job1", "group1")
.Build()
// Trigger the job to run now, and then repeat every second
let trigger =
TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(fun x ->
x.WithIntervalInSeconds(1).RepeatForever()
|> ignore)
.Build()
// Tell quartz to schedule the job using our trigger
let! _offset = scheduler.ScheduleJob(job, trigger)
// some sleep to show what's happening
do! Task.Delay(TimeSpan.FromSeconds(10))
// and last shut down the scheduler when you are ready to close your program
do! scheduler.Shutdown()
} |> ignore
Console.ReadLine() |> ignore
输出类似于:
4/16/2022 6:11:52 PM
4/16/2022 6:11:52 PM
4/16/2022 6:11:53 PM
4/16/2022 6:11:54 PM
4/16/2022 6:11:55 PM
4/16/2022 6:11:56 PM
4/16/2022 6:11:57 PM
4/16/2022 6:11:58 PM
4/16/2022 6:11:59 PM
4/16/2022 6:12:00 PM
4/16/2022 6:12:01 PM