using System;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
ITrigger Cron(string cronExpression) => TriggerBuilder.Create().WithCronSchedule(cronExpression).Build();
IScheduler sc = await StdSchedulerFactory.GetDefaultScheduler();
await sc.Start();
await sc.ScheduleJob(TaskJob.Build(() => Console.WriteLine($"hello{DateTime.Now}")), Cron("0/3 * * * * ?"));
await new Task(()=> { });
public class TaskJob : IJob
{
const string key = nameof(TaskJob);
static IJobDetail build(object o) => JobBuilder.Create<TaskJob>().SetJobData(new() { { key, o } }).Build();
public static IJobDetail Build(Func<Task> task) => build(task);
public static IJobDetail Build(Action action) => build(action);
async Task IJob.Execute(IJobExecutionContext context)
{
switch (context.MergedJobDataMap[key])
{
case Func<Task> t: await t(); break;
case Action a: a(); break;
default: throw new NotImplementedException();
}
}
}
https://stackoverflow.com/questions/27956401/schedule-an-action-in-quartz-net 참고함.
Java에서 건너온 라이브러리라 그런지 업데이트가 현제까지 활발함에도 불구하고 사용방법이 시대착오적이라 위와같이 별도의 구현이 필요함.
매 Execute마다 새로운 객체를 할당하기 때문에 class에 캐싱같은것도 못함.
하루이상 장시간에 걸친 스케줄링이 필요할때 쓰는용도임을 감안하고 참아야 할듯.
위 링크의 다른 답변에서 소개한 fluentscheduler가 모델상 좋아보이는데,
https://github.com/fluentscheduler/FluentScheduler/issues/214
개발자가 이런 아름다운 비전을 남겨놓은채로 2년째 부재중임.
https://github.com/fluentscheduler/FluentScheduler/issues/319
죽었는지 어쨋는지 아무도 모르는듯.
https://dev.to/tallesl
멀쩡하고만
https://gall.dcinside.com/board/view/?id=programming&no=2247305&page=1
생각해보니 쿼츠가 자바에서 건너온놈이라 디자인적 불협화음이 발생한듯함